Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.1.2
    • Labels:
      None

      Description

      Some uses of regexp patterns that include an unnamed capture group before a named one seems to cause the index of named groups to get out of sync (no longer retrieve proper matched value)

      eg: regular expression 'a(a|c)(?<named>b)'
      applied to input "acb" (no quotes)

      causes the function matcher.group("named") to return the value "c" instead of the expected value "b".

      Upon further inspection, it appears that the group name extractor function marks the group "named" as having index '1', by inserting it at the front of the list, but since the underlying regexp has '(a|c)' marked as group '1', and the "named" group marked as group '2', I can see that this is why the indices are out of sync.

      A workaround is to simply make all unnamed groups unmatching, ie using (?:) instead of ().

        Gliffy Diagrams

          Issue Links

            Activity

            Hide
            sysadmin System Administrator added a comment -

            Comment from Eric Bell

            I believe that a single change in the NamedPattern code would fix the above problem...

            private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(\\w+)>|\\(");

            (That way unnamed patterns are still counted but given "null" as their name)

            Show
            sysadmin System Administrator added a comment - Comment from Eric Bell I believe that a single change in the NamedPattern code would fix the above problem... private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile( "\\(\\?<(\\w+)>|\\(" ); (That way unnamed patterns are still counted but given "null" as their name)
            Hide
            sysadmin System Administrator added a comment -

            Comment from Eric Bell

            Sorry, I believe I made a mistake in my previous line...
            Unnamed patterns need to be exluded if they are marked as (?:
            So the line should be:

            private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(\\w+)>|(?!\\(\\?\\:)\\(");
            Show
            sysadmin System Administrator added a comment - Comment from Eric Bell Sorry, I believe I made a mistake in my previous line... Unnamed patterns need to be exluded if they are marked as (?: So the line should be: private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile( "\\(\\?<(\\w+)>|(?!\\(\\?\\:)\\(" );
            Hide
            sysadmin System Administrator added a comment -
            Show
            sysadmin System Administrator added a comment - Imported from http://code.google.com/p/named-regexp/issues/detail?id=4 Reported by Joel Merk
            Hide
            tony19 Anthony Trinh [Administrator] added a comment - - edited

            The group index calculation was indeed incorrect as you had pointed out. It's fixed in commit baa4d09a1ca3de4967e66efb7b36faa03331e38f.

            Show
            tony19 Anthony Trinh [Administrator] added a comment - - edited The group index calculation was indeed incorrect as you had pointed out. It's fixed in commit baa4d09a1ca3de4967e66efb7b36faa03331e38f .

              People

              • Assignee:
                tony19 Anthony Trinh [Administrator]
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 1 day
                  1d

                    Development

                      Who's Looking?