"sector 19" =~ /\d/ # Returns 7
Another cool thing you can do in Ruby is String.scan - this iterates though a string, matching against a regular expression and returns an array with all the matches.
# Returns all the <item> tags
If you know the right Java APIs though, these operations are almost as easy. The following Java code does the same thing as the above Ruby code:
ArrayList<String> tagList = new ArrayList<String>();
Matcher matcher =
String match = feedString.substring(matcher.start(),
A Matcher object is returned by Pattern.matcher - it basically remembers a string, a regular expression to match against, and the last matched position. Matcher.find returns true if there's another match. Matcher.start, and Matcher.end returns the start and end indexes of the previous match.
I'll admit that the code is a bit longer than the Ruby version, but it's still quite easy.