Ok so I have a phrase delimited by spaces. Each element can only be an integer or real number, with the exception of the last two elements that can be the string null. My regular expression is
([0-9]*\.[0-9]*|[0-9]*) ([0-9]*\.[0-9]*|[0-9]*) ([0-9]*\.[0-9]*|[0-9]*) ([0-9]*\.[0-9]*|[0-9]*|null) ([0-9]*\.[0-9]*|[0-9]*|null)
I do not understand why in the phrase
123 15 3 null null
The last null doesn’t get captured. Someone mentioned the use of ‘/’ and told me they were like quotes for regular expressions, but it seemed to me that adding that just must by regular expression search for that string. I am writing my regular expression in this form because I am implementing it into java and need to separate the groups or elements accordingly.
EDIT:
Thank you everyone for the great responses. Clearly I need more practice, and probably some sleep! My regular expression looks much cleaner now.
Revised Expression
(\d+\.\d+|\d+) (\d+\.\d+|\d+) (\d+\.\d+|\d+) (\d+\.\d+|\d+|null) (\d+\.\d+|\d+|null)
It’s due to the way you’re defining your or groups with the * modifier:
With the above statement, it’s possible to match nothing with [0-9]*. And it will match that first if it can.
When you have 2 in a row:
It’s now forced to match the ‘null’ in the first group, but can get away with matching nothing in the second.
If you were to modify these capture groups to:
They wouldn’t be able to match ‘nothing’ anymore, and would line up the way you want it to.
You could also flip it around, and force it to match to ‘null’ before nothing: