如何在找到匹配项后获取第一个单词?
例如,一旦找到Car
,如何获取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());
}
}
}