Question

I am developing a simple translator from MathML to Latex, using Lex and Yacc. In my lex file containing the regex rules I have one defined for arithmetic operators [-+*=/]. I want to extended so that it would recognize plus-minus (+-) and invisible times ('&InvisibleTimes'), but I'm unfamiliar with regex and I need some help.

Was it helpful?

Solution

Try this:

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

Note that you need to escape the + in +- because it's an operator outside of character classes. You can do this with backslash (as I've done here) or with double quotes. (The double-quote syntax is pretty unusual -- most other regex implementations only use backslash for escaping, so I'd be inclined to use backslashes as it makes the regex more "conventional".)

OTHER TIPS

Would something like this work?

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

I'm not very familiar with MathML, so I have the opposite problem of you. As others have said, you can do this all in one regex, like this:

[-+*=/]|\+-|&InvisibleTimes

However, if you want to have different actions associated with each of these, you need to do it like this:

[-+*=/]            {/* action 1 here */}
\+-                {/* action 2 here */}
&InvisibleTimes    {/* action 3 here */}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top