Java正则表达式匹配模式

标签 java regex

我需要根据某些文本检查模式(我必须检查我的模式是否在许多文本中)。

这是我的例子

String pattern = "^[a-zA-Z ]*toto win(\\W)*[a-zA-Z ]*$";    
if("toto win because of".matches(pattern))
 System.out.println("we have a winner");
else
 System.out.println("we DON'T have a winner");

对于我的测试,模式必须匹配,但使用我的正则表达式不匹配。 必须匹配:

" toto win bla bla"

"toto win because of"
"toto win. bla bla"


"here. toto win. bla bla"
"here? toto win. bla bla"

"here %dfddfd . toto win. bla bla"

不得匹配:

" -toto win bla bla"
" pretoto win bla bla"

我尝试使用我的正则表达式来完成此操作,但它不起作用。

你能指出我做错了什么吗?

最佳答案

这可行

(?im)^[?.\s%a-z]*?\btoto win\b.+$

说明

"(?im)" +         // Match the remainder of the regex with the options: case insensitive (i); ^ and $ match at line breaks (m)
"^" +             // Assert position at the beginning of a line (at beginning of the string or after a line break character)
"[?.\\s%a-z]" +    // Match a single character present in the list below
                     // One of the characters “?.”
                     // A whitespace character (spaces, tabs, and line breaks)
                     // The character “%”
                     // A character in the range between “a” and “z”
   "*?" +            // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
"\\b" +            // Assert position at a word boundary
"toto\\ win" +     // Match the characters “toto win” literally
"\\b" +            // Assert position at a word boundary
"." +             // Match any single character that is not a line break character
   "+" +             // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"$"               // Assert position at the end of a line (at the end of the string or before a line break character)

更新1

(?im)^[?~`'!@#$%^&*+.\s%a-z]*? toto win\b.*$

更新2

(?im)^[^-]*?\btoto win\b.*$

更新3

(?im)^.*?(?<!-)toto win\b.*$

说明

"(?im)" +       // Match the remainder of the regex with the options: case insensitive (i); ^ and $ match at line breaks (m)
"^" +           // Assert position at the beginning of a line (at beginning of the string or after a line break character)
"." +           // Match any single character that is not a line break character
   "*?" +          // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
"(?<!" +        // Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   "-" +           // Match the character “-” literally
")" +
"toto\\ win" +   // Match the characters “toto win” literally
"\\b" +          // Assert position at a word boundary
"." +           // Match any single character that is not a line break character
   "*" +           // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"$"             // Assert position at the end of a line (at the end of the string or before a line break character)
<小时/>

正则表达式需要转义才能在代码中使用

关于Java正则表达式匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993334/

相关文章:

java - 在DecimalFormat上将点更改为逗号

java - 表格未添加到操作面板

javascript - JS正则表达式通过 'value'获取路径名 'key'

java - 如何从字符串中提取图像url?

javascript - 使用正则表达式在字符之间提取数据?

java - 如何添加循环中的所有结果

java - 在 Hashmap 数组中搜索特定键 android

java - German Umlaute 的问题,从 SWI Prolog 到 JAVA 的 TCPIP

regex - 在 Lua 中使用正则表达式查找前导星号

javascript - 通过正则表达式拆分 JS 字符串?