Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8316805

For a regular expression with named groups, you can't extract the names.

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If you get a regular expression with possible named groups, you can't extract those names.

      This is a nice feature of Python where you can get those names. Even more, after analyzing an string, you can get a Dictionary mapping those named groups with their corresponding matches.

      But independently of that, when writing some methods receiving named regular expressions, I would like not to write the names in the method function, in order to ensure it is independent of the receiving regular expression.

      There are cases where this dependency is actually encouraged, if the method depends on the schema described by the pattern, and then yes, those names could be in the method body.

      Anyways, here is a Python example of this capability:

      ```
      import re

      # A pattern with named groups
      pattern = r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})"
      compiled_pattern = re.compile(pattern)

      # Get the names of the groups
      group_names = list(compiled_pattern.groupindex.keys())
      print(group_names)
      ```
      The output is
      ```
      ['year', 'month', 'day']
      ```


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: