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

String#getAt(Pattern, int) for Regex Matcher

Attach filesAttach ScreenshotAdd voteVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    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

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            kaendfinger Kenneth Endfinger

            Dates

              Created:
              Updated:

              Slack

                Issue deployment