2

Do apex regular expressions support named groups? Similar to http://download.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28java.lang.String%29?

Example (?<name>X)

The syntax seems to be supported but I couldn't find a way to reference groups by name: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_pattern_and_matcher_matcher_methods.htm

Pattern oTestPattern = Pattern.compile('(?<name>X)'); Matcher oMatcher = oTestPattern.matcher('X'); System.assert(false, oMatcher.groupCount());

2

Currently, as at Spring 15, the answer is no, you can't access the named groups in a Matcher even though they are valid in the RegEx.

There isn't a method exposed to access the capture group by name. There is group(Integer) to get the capture group by groupIndex. Note that group zero always stands for the entire expression.

If you try and call group(String) you will get an error

Method does not exist or incorrect signature: [Matcher].group(String) line:X

See also:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.