Regex for lex
I am developing a simple MathML to latex translator using Lex and Yacc. In my lex file containing the regex rules, I have one for the arithmetic operators [- + * = /]. I want to expand so that it recognizes plus or minus (+ -) and invisible times ('& InvisibleTimes'), but I am not familiar with regex and I need help.
a source to share
Try the following:
([-+*=/]|\+-|&InvisibleTimes)
Note that you need to avoid +
in +-
because it is an operator outside of character classes. You can do it with a backslash (as I did here) or with double quotes. (The double-quoted syntax is rather unusual - most other regex implementations only use backslashes for escaping, so I'll tend to use backslashes, as that makes the regex more "ordinary".)
a source to share
I am not very familiar with MathML, so I have the opposite problem. As others have said, you can do it all in one regex, for example:
[- + * = /] | \ + - | & InvisibleTimes
However, if you want to have different activities associated with each of them, you need to do it like this:
[- + * = /] {/ * action 1 here * /}
\ + - {/ * action 2 here * /}
& InvisibleTimes {/ * action 3 here * /}
a source to share