Wolfram Computation Meets Knowledge

StringPatternQ

Contributed by: Taliesin Beynon

Test a valid string pattern.

ResourceFunction["StringPatternQ"][expr]

gives True if expr is a valid string pattern, suitable for use with e.g. StringMatchQ.

Examples

Basic Examples

Test any pattern:

In[1]:=
ResourceFunction["StringPatternQ"]["a*"]
Out[1]=
In[2]:=
ResourceFunction["StringPatternQ"][h[x_]]
Out[2]=
In[3]:=
ResourceFunction["StringPatternQ"]["a" ~~ ___ ~~ "b"]
Out[3]=

More complex patterns:

In[4]:=
ResourceFunction["StringPatternQ"][__ ~~ PunctuationCharacter]
Out[4]=
In[5]:=
ResourceFunction["StringPatternQ"][x : WordCharacter ..]
Out[5]=

A pattern not suitable for strings:

In[6]:=
ResourceFunction["StringPatternQ"][Except[0, _Integer]]
Out[6]=

A regular expression:

In[7]:=
ResourceFunction["StringPatternQ"][RegularExpression["[a-z]+"]]
Out[7]=

Resource History