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.

0


a source to share


3 answers


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".)

+1


a source


Does something like this work?



(?:[-+*=/]|\+-|&InvisibleTimes)

      

+2


a source


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 * /}
0


a source







All Articles