Is there a function which checks the validity of a string to be used as argument to RegularExpression
?
Composing regular expressions is not an easy task. In Mathematica it becomes even more difficult, since all special characters have to be escaped, i.e. \
-> \\
and so on.
I am looking for a function that will return whether a string is valid as an argument to RegularExpression
, and possibly give an indication where there are syntactic, e.g. unbalanced parentheses, illegal operators and so on.
RegularExpression
is very often used inside StringPosition
or other functions. If I want to check the regular expression before using it, it simply evaluates itself instead of generating an error message:
RegularExpression["this is an unbalanced ("]
.
A regular expression validation function would only be half the battle, because the more important question of whether the regular expression serves its purpose can hardly be checked automatically. Syntactic correctness can certainly be checked automatically, and that is what I am after.
After all, such a function which I would name RegularExpressionQ[expr_String]
returning True
or False
- especially if it prints detailed error messages in addition if there are some - could be a great work-saver!