I'm looking for a way to capture a string out of a larger string that matches a regex. I looked into the String class, but there is nothing there to help. I can't believe a function like this isn't included in the class. I can match on it, but can't extract it:
It seems a fine separation of concerns that String doesn't know how to apply regular expressions to itself.
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Joined: Nov 17, 2003
Thanks! The function group() from matcher fits the bill.
Joined: May 06, 2004
Originally posted by Tyler Jordan: Thanks! The function group() from matcher fits the bill.
But your regex doesn't. The '^' and '$' anchors force the regex to match the entire string, but you obviously want to pluck out matching substrings. Also, there's no need for the "||" (or even "|") in the first character class; OR is implicit, and in fact a pipe just matches a pipe when you use it in a character class. Putting it all together: