21
$\begingroup$

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?

$\endgroup$
  • $\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 Feb 27 '16 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 Feb 27 '16 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 Feb 27 '16 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 Feb 29 '16 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$ – Oleksandr R. Sep 2 '16 at 10:11
8
$\begingroup$

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.

$\endgroup$

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.