我正在尝试用Java构建正则表达式字符串。
例
search - test
它应该匹配以下TestCases
1. The test is done
2. me testing now
3. Test
4. tEST
5. the testing
6. test now
我现在有什么(不工作)
([a-z]+)*[t][e][s][t]([a-z]+)*
什么是正确的正则表达式代码呢?
最佳答案
一种方法可以是这样调用String#matches
:
String search = "test";
String line = "The Testing";
boolean found = line.matches("(?i)^.*?" + Pattern.quote(search) + ".*$"); // true
在这里,
(?i)
用于忽略大小写匹配,而Pattern.quote
用于从search
字符串转义可能的正则表达式特殊字符。