title: java regex preferences when multiple matches occur
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I have tried these 2 patterns:</p>
  
  <pre><code>pattern1 = Pattern.compile("\\d+|\\d+/\\d+"))
  pattern2 = Pattern.compile("\\d+/\\d+|\\d+"))
  </code></pre>
  
  <p>In English, pattern1 says "digits OR digits/digits", whereas pattern2 says "digits/digits OR digits".</p>
  
  <p>Now consider this input string: </p>
  
  <pre><code>inputStr = "blah... 231/232 blah... 4 blah... 2"
  </code></pre>
  
  <p>For pattern1, I found these matches:</p>
  
  <pre><code>[junit] found 231
  [junit] found 232
  [junit] found 4
  [junit] found 2
  </code></pre>
  
  <p>For pattern2, I found these matches:</p>
  
  <pre><code>[junit] found 231/232
  [junit] found 4
  [junit] found 2
  </code></pre>
  
  <p>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.</p>
  
  <p>So the most important question for me is this:
  Is this behaviour reliable/predictable, or is it going to be different for different platforms? </p>
  
  <p>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?</p>