This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Tyler Jordan
Ranch Hand
Joined: Nov 17, 2003
Posts: 70
posted
0
Thanks! The function group() from matcher fits the bill.
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted
0
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: