Function Repository Resource:

StringPatternQ

Source Notebook

Test whether an expression is valid string pattern

Contributed by: Taliesin Beynon

ResourceFunction["StringPatternQ"][expr]

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

Examples

Basic Examples (4) 

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

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 10 January 2019

License Information