# Can Mathematica convert a RegularExpression into a StringExpression or vice-versa?

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

RegularExpression["\\\\newcommand({([^{}]|(?1))*})(?1)"]


Could it be converted into a StringExpression automatically?

• 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. Commented Feb 27, 2016 at 4:47
• The internal function StringPatternPatternConvert 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. Commented Feb 27, 2016 at 4:55
• @MarcoB I do not recall reading of an inverse function to StringPatternPatternConvert 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. Commented Feb 27, 2016 at 9:48
• @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. Commented Feb 29, 2016 at 4:23
• 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. Commented Sep 2, 2016 at 10:11

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 StringPatternPatternConvert:
StringPatternPatternConvert[StartOfString ~~ "a" ~~ __ ~~ "b" ~~ EndOfString]

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