Hi I'm trying to understand in particular how does the OR operator "|" work in java regex. I'm just starting to use it and most of the things are unclear to me.
Suppose I wish to match fractions and integers, that is to say things of the form 1/2, 12/25, and also things of the form 13, 235, etc.
I have tried these 2 patterns:
pattern1 = Pattern.compile("\\d+|\\d+/\\d+"))
pattern2 = Pattern.compile("\\d+/\\d+|\\d+"))
In English, pattern1 says "digits OR digits/digits", whereas pattern2 says "digits/digits OR digits".
Now consider this input string:
inputStr = "blah... 231/232 blah... 4 blah... 2"
For pattern1, I found these matches:
[junit] found 231
[junit] found 232
[junit] found 4
[junit] found 2
For pattern2, I found these matches:
[junit] found 231/232
[junit] found 4
[junit] found 2
Now the only difference between pattern1 and pattern2 is the orders of its matched elements. Of course pattern2 is the one I wanted, as it seems to "prefer" a real faction than to take them apart.
So the most important question for me is this: Is this behaviour reliable/predictable, or is it going to be different for different platforms?
But also just curious... this question too: I also find it confusing because the operator "OR" should be symmetric with regard to its arguments, like addition. You'd imagine people be worried when 1+2 and 2+1 carries different semantics... is there any reason for pattern1 and pattern2 here to be semantically different?