【发布时间】:2012-03-15 15:47:37
【问题描述】:
假设我有一个String,比如one two three one one two one。
现在我使用Pattern 和Matcher 在String 中查找任何特定的Pattern。
像这样:
Pattern findMyPattern = Pattern.compile("one");
Matcher foundAMatch = findMyPattern.matcher(myString);
while(foundAMatch.find())
// do my stuff
但是,假设我想找到多种模式。对于我采取的示例String,我想同时找到one 和two。现在它是一个非常小的字符串,所以可能的解决方案是使用另一个 Pattern 并找到我的匹配项。但是,这只是一个小例子。有没有一种有效的方法来做到这一点,而不是在所有Patterns 集合中循环尝试?
【问题讨论】: