2

I am trying to write a Regular Expression to read real literals based on this graph: Regex Graph
I found that this solution works:

([0-9]+(\.[0-9]+)?[Ee][\+\-]?[0-9]+)|([0-9]+\.[0-9]+)

Notice the two parts separated via alternation:

([0-9]+(\.[0-9]+)?[Ee][\+\-]?[0-9]+) 
([0-9]+\.[0-9]+)

When I tried to switch the order of which one is on which side of the alternation symbol, it stops working

([0-9]+\.[0-9]+)|([0-9]+(\.[0-9]+)?[Ee][\+\-]?[0-9]+)

These parts are exactly the same as above, but now it's suddenly not working. Am I misunderstanding how the | (alternation) symbol works? Is it not the same as || (or) for boolean expressions?

Caleb Robinson
  • 1,010
  • 13
  • 21
  • 3
    It will match the first pattern it finds in the alternation. So, if you have `aab|aa` and you try it against `xxaabxx` you'd get `aab` out of it. If you flip them around, you'd receive `aa`. – VLAZ Feb 17 '20 at 17:03
  • @VLAZ That seems so obvious now. Thank you so much! – Caleb Robinson Feb 17 '20 at 17:04

0 Answers0