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

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 (5) 

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]=

Use a relative context:

In[8]:=
ResourceFunction["PossibleNameQ"]["`x"]
Out[8]=
In[9]:=
ResourceFunction["PossibleNameQ"]["`Private`x"]
Out[9]=
In[10]:=
ResourceFunction["PossibleNameQ"]["``x"]
Out[10]=

Properties and Relations (3) 

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

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

It doesn’t need to be an existing symbol:

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

PossibleNameQ only tests if it can be a symbol:

In[13]:=
Symbol["This`Probably`Does`Not`Exist"]
Out[13]=
In[14]:=
Head[%]
Out[14]=
In[15]:=
ResourceFunction["PossibleNameQ"]["This`Can't`Exist"]
Out[15]=
In[16]:=
Symbol["This`Can't`Exist"]
Out[16]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.0 – 22 November 2021

Related Resources

License Information