Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be the best way to detect if a pattern corresponds to a string pattern ? An invalid string pattern p would lead to an error in StringMatchQ[string, p] for example.

share|improve this question
Have a look at System`Dump`validStringExpressionQ[]. – J. M. Nov 20 '12 at 9:46
Yes that's the kind of built-in function I was looking for thanks. Maybe you could put the comment as an answer. – faysou Nov 20 '12 at 9:51
up vote 8 down vote accepted

At Faysal's behest: there's the (undocumented) function System`Dump`validStringExpressionQ[]:


System`Dump`validStringExpressionQ[_ ~~ 1]
share|improve this answer
A look at ??System`Dump`validStringExpressionQ is particularly enlightening... – J. M. Nov 20 '12 at 10:01
Indeed so basically the pattern is passed to StringMatchQ in the general case and the function checks that no error occured. – faysou Nov 20 '12 at 10:31
+1. A look at the implementation reveals some unpleasant behavior,e.g. stpatt = "**"; System`Dump`validStringExpressionQ[stpatt] returns False, while System`Dump`validStringExpressionQ[Evaluate[stpatt]] returns True. I would prefer to get True in both cases. – Leonid Shifrin Nov 20 '12 at 10:33
StringPatternQ[_String | _StringExpression | _RegularExpression] = True;
StringPatternQ[_] = False;
share|improve this answer

There is the internal GeneralUtilities`StringPatternQ:

? GeneralUtilities`StringPatternQ

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

It is a predicate that allows for the most inclusive match, thus it returns True for any pattern that could stand for a string pattern.

 GeneralUtilities`StringPatternQ[_]   (* ==> True *)

However, it is less precise as System`Dump`validStringExpressionQ:

patt = {"s", "*", "**", __, Except@_, x, _ ~~ 1};
GeneralUtilities`StringPatternQ /@ patt
System`Dump`validStringExpressionQ /@ patt
Quiet@Check[StringMatchQ["s", #], $Failed] & /@ patt
{True, True, True, True, True, False, False}
{True, True, True, True, False, False, False}
{True, True, True, True, $Failed, $Failed, $Failed}

Leonid's example:

stpatt = "**";
System`Dump`validStringExpressionQ[stpatt]           (* ==> False *)
System`Dump`validStringExpressionQ[Evaluate[stpatt]] (* ==> True *)
GeneralUtilities`StringPatternQ[stpatt]              (* ==> True *)
GeneralUtilities`StringPatternQ[Evaluate[stpatt]]    (* ==> True *)

According to its PrintDefinitions, it recognizes the following arguments as string patterns:

spq[e_Alternatives] := AllTrue[e, spq];
spq[e_List] := AllTrue[e, spq];
spq[_String] := True;
spq[DatePattern[e_List]] := Developer`StringVectorQ[e];
spq[DatePattern[e_List, sep_]] := Developer`StringVectorQ[e] && spq[sep];
spq[Verbatim[Longest][e_]] := spq[e];
spq[Verbatim[Shortest][e_]] := spq[e];
spq[Verbatim[Repeated][e_, ___]] := spq[e];
spq[Verbatim[RepeatedNull][e_]] := spq[e];
spq[Verbatim[Except][e_]] := spq[e];
spq[Verbatim[Except][e_, f_]] := spq[e] && spq[f];
spq[Verbatim[Pattern][_, patt_]] := spq[patt];
spq[Verbatim[Verbatim][_String]] := True;
spq[Verbatim[Condition][patt_, _]] := spq[patt];
spq[Verbatim[PatternTest][patt_, _]] := spq[patt];
spq[e_AnyOrder] := AllTrue[e, spq];
spq[e_FixedOrder] := AllTrue[e, spq];
spq[expr_] := MatchQ[expr, $AtomicPatterns];
share|improve this answer
The $AtomicPatterns is GeneralUtilities`Strings`PackagePrivate`$AtomicPatterns. – Karsten 7. Nov 28 '15 at 10:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.