【问题标题】:Java equivalent of JavaScript's String.match()Java 等效于 JavaScript 的 String.match()
【发布时间】:2010-11-29 18:02:42
【问题描述】:
【问题讨论】:
标签:
java
javascript
regex
【解决方案1】:
查看Regex tutorial
您的代码应该类似于以下内容:
String input = "The quick brown fox jumps over the lazy dog";
Matcher matcher = Pattern.compile("e").matcher(input);
while ( matcher.find() ) {
// Do something with the matched text
System.out.println(matcher.group(0));
}
【解决方案2】:
查看regex 包中的Pattern 和Matcher 类。特别是Matcher.find 方法。这不会返回数组,但您可以在循环中使用它来遍历所有匹配项。
【解决方案3】:
String.matches(String regex) 是更直接的等价物,但仅用于一次性正则表达式。如果您要多次使用它,请按照建议使用Pattern.compile。