I'm converting a Java 7 application to a Java 6 application. I'm stuck on the matcher.group
property, since I don't really understand what it does.
I have the following code:
public static final String PropertyRegexPrefixGroup = "prefix";
public static final String PropertyRegexPostfixGroup = "postFix";
public static ParsedProperty parse(String property) {
Matcher matcher = PropertyRegex.matcher(property);
boolean isMatch = matcher.matches();
if (!isMatch)
return new ParsedProperty(null, property, false);
String prefix = matcher.group(PropertyParser.PropertyRegexPrefixGroup);
String postfix = matcher.group(PropertyParser.PropertyRegexPostfixGroup);
return new ParsedProperty(prefix, postfix, true);
}
I have to rewrite the String prefix
and String postfix
property declarations. How can I achieve this?
In Java 6, you can't have named groups, so you need to reference the groups by index.
Given this pattern:
Pattern pattern = Pattern.compile("(foo).*(bar)")
and this code:
Matcher matcher = pattern.matcher("foo....something...else...bar");
matcher.find();
matcher.group(0)
matches the entire String, matcher.group(1)
returns "foo" and matcher.group(2)
returns "bar"