Java - 用于搜索的正则表达式

标签 java regex

我正在尝试用 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/

相关文章:

java - DispatcherServlet noHandlerFound

用于分析文件的php脚本

sql - 在字符前后使用 regexp_replace 替换字符

java - 将类传递给 Maven2 插件

java - cqengine:maven 对 sqllite 的依赖是否必要?

java - 必须使用 UTF-16 Url 编码才能在 Java 中提交搜索。我怎么能够?

php - 如何使用 PHP 删除回车符?

python - 在 python 中使用正则表达式将 URL 替换为链接

正则表达式删除单词,如果它在 R 中同时多次包含字母/特殊字符

java - 实践中的并发设计原则