Using java.util.regex.Matcher, are the following expressions for matched1 and matched2 equivalent? Is there a simpler way to get a value for matched?
StringBuilder b = ...
Pattern p = ...
Matcher m = p.matcher(b);
m.find();
String matched1 = b.substring(m.start(), m.end());
String matched2 = m.group();
Yes, they are equivalent, and no, there is no simpler way.
I’d prefer the
.group()approach; perhaps wrapped in anif (m.find())conditional.