-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
None
-
generic
-
generic
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']
```
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']
```
- duplicates
-
JDK-8065554 MatchResult should provide values of named-capturing groups
- Resolved