我正在尝试用 Java 构建一个正则表达式字符串。
例子
search - test
它应该匹配下面的测试用例
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
字符串中转义可能的正则表达式特殊字符.
关于Java - 用于搜索的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250769/