Java相当于JavaScript的String.match()
JavaScript的String.match()
的Java等价物是什么
我需要得到一个数组或所有匹配的列表
例:
var str = 'The quick brown fox jumps over the lazy dog'; console.log(str.match(/e/gim));
给
["e", "e", "e"]
http://www.w3schools.com/jsref/jsref_match.asp
检查Regex教程
您的代码应该与此类似:
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)); }
看一下regex
包中的Pattern
和Matcher
类。 特别是Matcher.find
方法。 这不会返回数组,但您可以在循环中使用它来遍历所有匹配。
String.matches(String regex)
是一个更直接的等价物,但只用于一次性正则表达式。 如果您将多次使用它,请按照建议坚持使用Pattern.compile
。