Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
2  
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

2 Answers 2

up vote 6 down vote accepted

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

System`Dump`validStringExpressionQ["**"]
   True

System`Dump`validStringExpressionQ[_ ~~ 1]
   False
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  
+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

Your Answer

 
discard

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.