Wolfram Research

Function Repository Resource:

PossibleNameQ

Source Notebook

Test if an expression corresponds to a valid symbol name

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["PossibleNameQ"][expr]

gives True if expr is a string corresponding to a valid symbol and False otherwise.

Details and Options

In ResourceFunction["PossibleNameQ"]["string"], the given string need not correspond to an existing symbol; instead, it only needs to be possible that it could be a symbol.

Examples

Basic Examples

Test if a string is a valid symbol name:

In[1]:=
ResourceFunction["PossibleNameQ"]["x"]
Out[1]=
In[2]:=
ResourceFunction["PossibleNameQ"]["123"]
Out[2]=

Test a fully qualified name:

In[3]:=
ResourceFunction["PossibleNameQ"]["System`Plot"]
Out[3]=
In[4]:=
ResourceFunction["PossibleNameQ"]["System!`Plot"]
Out[4]=

Non-string expressions cannot be symbol names:

In[5]:=
ResourceFunction["PossibleNameQ"][1]
Out[5]=

Test a string corresponding to a nonexistent symbol:

In[6]:=
ResourceFunction["PossibleNameQ"]["This`Probably`Does`Not`Exist"]
Out[6]=
In[7]:=
NameQ["This`Probably`Does`Not`Exist"]
Out[7]=

Properties and Relations

PossibleNameQ only tests if the string corresponds to a valid symbol name:

In[8]:=
ResourceFunction["PossibleNameQ"]["This`Probably`Does`Not`Exist"]
Out[8]=

It does not need to be an existing symbol:

In[9]:=
NameQ["This`Probably`Does`Not`Exist"]
Out[9]=

PossibleNameQ only tests if it can be a symbol:

In[10]:=
Symbol["This`Probably`Does`Not`Exist"]
Out[10]=
In[11]:=
Head[%]
Out[11]=
In[12]:=
ResourceFunction["PossibleNameQ"]["This`Can't`Exist"]
Out[12]=
In[13]:=
Symbol["This`Can't`Exist"]
Out[13]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information