I am familiar with regular expressions in other languages (e.g., Perl, Python, ViM), but the Mathematica StringExpression is baffling me.

I'd like to match strings that look like this:


The regular expression I'd use in Python/Perl would look like this: [0-9]+\.2[0-9]{2}nc. What is the equivalent in Mathematica StringExpression?

Related: Can I use a RegularExpression wherever a StringExpression is expected?

  • $\begingroup$ StringExpressions are converted into RegularExpressions. I personaly prefer RegularExpressions form, it much more universal and easy to make your code readable by non Mathematica programmers. $\endgroup$ – Murta Mar 7 '15 at 7:28
  • $\begingroup$ mathematica.stackexchange.com/questions/25677 $\endgroup$ – Murta Mar 7 '15 at 7:34

For me (that is very personal indeed), StringExpressions in Mathematica are much more transparant than regular expressions. Here are two StringExpressions for your strings:

p1 = NumberString ~~  ".2" ~~ DigitCharacter ~~ DigitCharacter ~~ "nc";
p2 = NumberString ~~"." ~~ (x : NumberString /; 200 <= ToExpression[x] < 300) ~~ "nc";

teststrings = {"1001.200nc", "12345.220nc", "987654.215nc"};
StringMatchQ[teststrings, p1]
StringMatchQ[teststrings, p2]

(* {True, True, True}
{True, True, True} *)
| improve this answer | |
  • $\begingroup$ What confuses me about StringExpressions are the multitude of ~. Are they separators? Do they indicate a pattern? I'm not sure what they mean. $\endgroup$ – jlconlin Mar 5 '15 at 17:15
  • $\begingroup$ @Jeremy. The double ~~ is just an infix notation for StringExpression, so in between the ~~ we have the arguments of the function StringExpression. That is the technical explanation. More friendly is to read it as 'followed by'. So my first pattern consists of a pattern for a numberstring, followed by the string ".2", followed by a digit, followed by a digit, followed by the string "nc". In the second StringExpression, between the brackets you see a pattern for a numberstring that has to satisfy a condition, just as with normal pattern matching in Mathematica. $\endgroup$ – Fred Simons Mar 5 '15 at 17:38
  • 1
    $\begingroup$ @Jeremy As Fred says, a~~b~~c is really StringExpression[a, b, c]. $\endgroup$ – chuy Mar 5 '15 at 17:39
  • $\begingroup$ slight variation: p1 = NumberString ~~ ".2" ~~ Repeated[DigitCharacter, 2] ~~ "nc" $\endgroup$ – Mike Honeychurch Mar 5 '15 at 22:53

Replace \. with \\., and wrap your regular expression with quotes "..." and RegularExpression:

regex = RegularExpression["[0-9]+\\.2[0-9]{2}nc"]

strings = {"1001.200nc", "12345.220nc", "987654.215nc", "nomatch"};

StringMatchQ[strings, regex]
(* {True, True, True, False} *)

StringCases[strings, regex]
(* {{"1001.200nc"}, {"12345.220nc"}, {"987654.215nc"}, {}} *)
| improve this answer | |
  • $\begingroup$ So can I use regex inlace of a StringExpression? $\endgroup$ – jlconlin Mar 5 '15 at 16:42
  • $\begingroup$ @Jeremy, afaik yes (with minor modifications as in the example above). $\endgroup$ – kglr Mar 5 '15 at 16:44
  • $\begingroup$ Jeremy, in case you haven't, see also the tutorial :Working with String Patterns $\endgroup$ – kglr Mar 5 '15 at 16:51
  • $\begingroup$ No I haven't seen that. Thanks for pointing that out. I'm reading it now. $\endgroup$ – jlconlin Mar 5 '15 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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