Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6805

String#getAt(Pattern, int) for Regex Matcher

    XMLWordPrintableJSON

Details

    Description

      Add String#getAt(Pattern, int) and String#getAt(String, int) extension methods.

      This will be similar to

      StringGroovyMethods.java
      public static String getAt(String self, Pattern pattern, int index) {
          Matcher matcher = pattern.matcher(self);
          if (!matcher.find()) {
              return null;
          }
          return matcher.group(index + 1); /* I think adding 1 is required, it may not be though to mimic Ruby */
      }
      
      public static String getAt(String self, String pattern, int index) throws PatternSyntaxException {
          return getAt(self, Pattern.compile(pattern), index);
      }
      
      Usage.groovy
      assert "Hello".[/(H)ello/, 0] == "H"
      

      The goal of this is to mimic Ruby's Behavior.

      NOTICE: I probably have an error in my implementation, but hopefully you get the gist of it.

      ALSO
      We could also add named capturing groups support (aka getAt(Pattern, String))

      Gist

      Attachments

        Activity

          People

            Unassigned Unassigned
            kaendfinger Kenneth Endfinger
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: