title: How to pipeline/chained regex expressions
body: <p>I have to extract the measure units and quantities from String texts. Like in this samples:</p>
  
  <pre><code>Original String // result:
  
  abc1mgabc // extract 1 and mg separately
  abc100mlabc //100 and ml
  abc256kgabc //256 and kg
  </code></pre>
  
  <p>Until now in a first moment I use this regular expression:</p>
  
  <pre><code>(?i)\d{1,5}(mg|g|gr|kg|ml|l)
  </code></pre>
  
  <p>to extract Quantity and Unit and store into <code>quant_unit</code> String.</p>
  
  <p>after that, I apply over <code>quant_unit</code> this two regular expressions: <code>\d</code> and <code>(?i)(mg|g|gr|kg|ml|l)</code> to extract respectively "quantity" and "measure unit". </p>
  
  <p>But I think it must have a way to extract it separately by applying just a regular expression (for each item to extract) over the original string?</p>
  
  <p>I though use some kind like: </p>
  
  <p>original_string -> applyRegex (to extract measure and unit) -> applyRegex2 (to extract measure or unit from that).</p>
  
  <p>Using its own regular expression or the Pattern class from Java.</p>
  
  <p>I'm create a enum to easy access of Patterns:</p>
  
  <pre><code>public enum Patterns {
  
      //the expression is: (?i)\d{1,5}(mg|g|gr|kg|ml|l)
      QUANTITY_UNIT("(?i)\\d{1,5}(" + MeasureUnit.getRegex() + ")"),
      QUANTITY("\\d"),
      UNIT("(?i)(" + MeasureUnit.getRegex() + ")");
  
      private Pattern pattern;
  
      Patterns(String patternString) {
          System.out.println(patternString);
          pattern = pattern.compile(patternString);
      }
  
      public Pattern getPattern() {
          return pattern;
      }
  
      public Matcher getMatcher(CharSequence input) {
          return getPattern().matcher(input);
      }
  
      public String findGroup(CharSequence input) {
          Matcher matcher = getMatcher(input);
          matcher.find();
          return matcher.group();
  
      }
  </code></pre>
  
  <p>And the Unit Test for the desired behaviour:</p>
  
  <pre><code>public class PatternsTest {
  
  @Test
  public void quantityUnit() {
      String testString = "abc1kgabc1l";
      String fg = Patterns.QUANTITY_UNIT.findGroup(testString);
      Assert.assertEquals("1KG", fg);
  }
  
  
  @Test
  public void quantity() {
      String testString = "abc1kgabc1l";
      String fg = Patterns.QUANTITY.findGroup(testString);
      Assert.assertEquals("1", fg);
  }
  
  
  @Test
  public void unity() {
      String testString = "abc1kgabc1l";
      String fg = Patterns.UNIT.findGroup(testString);
      Assert.assertEquals("kg", fg);
  }
  
  }
  </code></pre>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I made some refactoring based on commentaries e answers, now it works fine:</p>
  
  <pre><code>public enum Patterns {
  
      QUANTITY_UNIT("(?i)([0-9]+)(" + MeasureUnit.getRegex() + ")");
  
      private Pattern pattern;
  
      Patterns(String patternString) {
          pattern = pattern.compile(patternString);
      }
  
      public Pattern getPattern() {
          return pattern;
      }
  
      public Matcher getMatcher(CharSequence input) {
          return getPattern().matcher(input);
      }
  
      public String getQuantity(CharSequence input) {
          final int group_idx = 1;
  
          Matcher matcher = getMatcher(input);
          boolean found = matcher.find();
          return found ? toLower(matcher.group(group_idx)) : "";
      }
  
      private String toLower(String input) {
          return input.toLowerCase();
      }
  
      public String getUnity(CharSequence input) {
          final int group_idx = 2;
  
          Matcher matcher = getMatcher(input);
          boolean found = matcher.find();
          return found ? toLower(matcher.group(group_idx)) : "";
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>the test:</h2>
  
  <pre><code>public class MeasureUnityTest {
  
  
      @Test
      public void quantity() {
          String testString = "abc1kgabc1l";
          String fg = QUANTITY_UNIT.getQuantity(testString);
          Assert.assertEquals("1", fg);
      }
  
  
      @Test
      public void unity() {
          String testString = "abc1kgabc1l";
          String fg = QUANTITY_UNIT.getUnity(testString);
          Assert.assertEquals("kg", fg);
      }
  
      @Test
      public void unityUpperCase() {
          String testString = "abc1KGabc1l";
          String fg = QUANTITY_UNIT.getUnity(testString);
          Assert.assertEquals("kg", fg);
      }
  
  
      @Test
      public void unityNoOccurrence() {
          String testString = "fasfasfasfaf";
          String fg = QUANTITY_UNIT.getQuantity(testString);
          Assert.assertEquals("", fg);
      }
  
      @Test
      public void unityEmptyString() {
          String testString = "";
          String fg = QUANTITY_UNIT.getQuantity(testString);
          Assert.assertEquals("", fg);
      }
  
      /* If more than one matches, return the first*/
      @Test
      public void unityMoreThanOne() {
          String testString = "abc5mlabc5kg";
          String fg = QUANTITY_UNIT.getUnity(testString);
          Assert.assertEquals("ml", fg);
      }
  
      /* If more than one matches, return the first*/
      @Test
      public void quantityMoreThanOne() {
          String testString = "abcm5mlabc1kg";
          String fg = QUANTITY_UNIT.getQuantity(testString);
          Assert.assertEquals("5", fg);
      }
  
  }
  </code></pre>