Thursday, 8 August 2013

Finding first word after regex match

Finding first word after regex match

How do I grab the first word after my match?
For example, once I find Car, how do I grab Chevy?
public class NewExtractDemo {
public static void main(String[] args) {
String input = "I have the following Car: Chevy, Truck: Ford, Van:
Honda";
Pattern p = Pattern.compile("(Car|Truck|Van)");
Matcher m = p.matcher(input);
List<String> Search = new ArrayList<String>();
while (m.find()) {
System.out.println("Found a " + m.group() + ".");
Search.add(m.group());
}
}
}

No comments:

Post a Comment