title: Java Regex Pattern doesn't do what the online testing tools say
body: <p>I have a problem with a regular expression in Java. The following should be matched</p>
  
  <pre><code>2x 1 piece
  63x 9 pieces
  4x 1 piece
  1 piece
  23 pieces
  </code></pre>
  
  <p>with this regex:  </p>
  
  <pre><code>((\w+)x\s)*(\w+)\s*(\w*)
  </code></pre>
  
  <p>As we all know, we have to escape strings in Java. I escaped the regex and I tried to use this one: </p>
  
  <pre><code>String regex = "((\\w+)x\\s)*(\\w+)\\s*(\\w*)";
  </code></pre>
  
  <p>Now comes my problem: All online services for regular expressions mark my pattern as valid, except the ones for java. They don't mark what could be false so I can't really see my problem. This is the code I'm trying to use in Java:</p>
  
  <pre><code>String regex = "((\\w+)x\\s)*(\\w+)\\s*(\\w*)";
  Pattern r = Pattern.compile(regex);
  Matcher m = r.matcher(someClassWithMethods.text());
  int multiplier=0;
  int value= 0;
  String supplement = "";
  if (m.find( )) {
      multiplier= Integer.parseInt(m.group(2));
      value= Integer.parseInt(m.group(3));    
      supplement = m.group(4);
  }
  </code></pre>
  
  <p>I debugged the whole thing to see what's going on and all variables are as expected but I still get an empty group. What's wrong with this regex?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've changed some few things due to the comments and I've catched my NumberException with an additional if clause. Now I still don't get a matching result. What could that be?
  There's my new code:</p>
  
  <pre><code>String regex = "(?:(\\w+)x\\s)?(\\d+\\s+)(pieces?)";
  Pattern r = Pattern.compile(regex);
  Matcher m = r.matcher(quantityCell.text());
  int quantityMultiplier = 0;
  int quantity = 0;
  String supplement = "";
  if (m.find( )) {
      if(m.group(1) != null){ 
              quantityMultiplier = Integer.parseInt(m.group(1));
      }
      quantity = Integer.parseInt(m.group(2));    
      supplement = m.group(3);
  }
  </code></pre>