I know that Mathematica can perform string comparison using either StringExpression or RegularExpression. Although regular expressions are more common, I feel that the Mathematica StringExpressions are more intuitive. So is it possible to ask Mathematica to convert from one to the other?

For example, this regular expression was found from another thread


Could it be converted into a StringExpression automatically?

  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Commented Feb 27, 2016 at 4:47
  • 10
    $\begingroup$ The internal function StringPattern`PatternConvert returns the regular expression corresponding to a StringExpression. See Working with String Patterns. I seem to recall mention on this site of an equally obscure function capable of doing the opposite conversion, but I can't find it any more. $\endgroup$
    – MarcoB
    Commented Feb 27, 2016 at 4:55
  • 7
    $\begingroup$ @MarcoB I do not recall reading of an inverse function to StringPattern`PatternConvert and I cannot think of a good reason for one to exist in the system, since StringExpression is converted into a regular expression, not the other way around. Nevertheless I will be pleased if I am proven wrong. $\endgroup$
    – Mr.Wizard
    Commented Feb 27, 2016 at 9:48
  • $\begingroup$ @Mr.Wizard After spending quite some time scouring the site and the net for the elusive counter to PatternConvert, I have come to the conclusion that my original recollection must be wrong, and in fact there had been no mention of such a function. As you said, this stands to reason anyway, since the system converts from string expressions to regex. $\endgroup$
    – MarcoB
    Commented Feb 29, 2016 at 4:23
  • 3
    $\begingroup$ I think it is important to note that, while I agree with you that string expressions are more intuitive than regular expressions for people who don't use the latter every day, because string patterns are converted into regular expressions and matched using PCRE, they are subject to some strange and perhaps unexpected limitations versus ordinary patterns. One can easily be trapped by the generality of expression patterns and write a string pattern that can't actually be expressed as a regular expression. So, perhaps it's better that these two aren't treated as being completely interchangeable. $\endgroup$ Commented Sep 2, 2016 at 10:11

1 Answer 1


The conclusion of Mr. Wizard's and MarcoB's comments is that there is no function to convert regular expressions into string patterns, but there is a function to convert string patterns into regular expressions called StringPattern`PatternConvert:

StringPattern`PatternConvert[StartOfString ~~ "a" ~~ __ ~~ "b" ~~ EndOfString]

{"(?ms)\\Aa.+b\\z", {}, {}, Hold[None]}

A function to convert regular expressions into string patterns is not to be hoped for, since Mathematica does not need it.

  • $\begingroup$ To add something to a correct answer: Note that Mathematica is based on PCRE, which offers constructs which are not the regular expressions known from computer science, but more powerful constructs (-> "context-free" grammars). $\endgroup$ Commented Nov 28, 2023 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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