简体   繁体   English

如何在java中使用正则表达式来匹配单词

[英]How to use regex in java to match a word

I am trying to match a word (Source Ip) where each letter can be small or capital letter so i have wrote a regex pattern down but my m.find() is showing false even for Correct Match... 我试图匹配一个单词(Source Ip),其中每个字母可以是小字母或大写字母,所以我写了一个正则表达式模式,但我的m.find()即使对于正确匹配也显示为假...

Is there any wrong in my regex pattern or the way I have written is wrong? 我的正则表达式模式或我写的方式有错吗?

  String word = "Source Ip";
    String pattern = "[S|s][O|o][U|u][R|r][C|c][E|e]\\s*[I|i][P|p]";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(word);
    System.out.println(m.find());

You can simple use 你可以简单地使用

String pattern = "SOURCE\\s*IP";
Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);

Pattern.CASE_INSENSITIVE will make the matching case insensitive. Pattern.CASE_INSENSITIVE将使匹配大小写不敏感。

You don't need to alternate all letters between upper case and lowercase (note, as mentioned by others, the character class idiom does not require | to alternate - adding it between square brackets will also match the | literal). 你不需要在大写和小写之间交替所有字母(注意,如其他人所提到的,字符类成语不需要|交替 - 在方括号之间添加它也将匹配| literal)。

You can parametrize your Pattern initialization with the Pattern.CASE_INSENSITIVE flag (alternative inline usage would be (?i) before your pattern representation). 您可以使用Pattern.CASE_INSENSITIVE标志参数化Pattern初始化(?i)在模式表示之前,替代内联使用将是(?i) )。

For instance: 例如:

Pattern.compile("(?i)source\\s*ip");

... or ... ... 要么 ...

Pattern.compile("source\\s*ip", Pattern.CASE_INSENSITIVE);

Note 注意

Flag API here . 这里有 Flag API。

This solution has the problem of accepting sourceip as well. 该解决方案也存在接受sourceip的问题。

source\s*ip

正则表达式可视化

Debuggex Demo Debuggex演示

Therefore the correct answer should be 因此, 正确的答案应该是

source\s+ip

正则表达式可视化

Debuggex Demo Debuggex演示

to force the presence of at least one whitespace between the two words. 强制在两个单词之间至少存在一个空格。

To use this expression in Java you have to escape the backslash and use something like: 要在Java中使用此表达式,您必须转义反斜杠并使用以下内容:

Pattern.compile("source\\s+ip", Pattern.CASE_INSENSITIVE);

if you really want to use regex though for some reason and not pattern. 如果你真的想使用正则表达式虽然出于某种原因而不是模式。 methods then this regex should suit your needs and it works just fine in java for me 方法然后这个正则表达式应该适合你的需要,它在我的java工作正常

 [s|S][o|O][U|u][r|R][c|C][e|E][ ]*[i|I][p|P]

your case of using 你使用的情况

\\s*

won't identify spaces however this will 不会识别空间,但这会

 \s*

you had one slash too many :) 你有一个斜线太多了:)

EDIT: I demonstrate my ignorance, after checking regexpal I was wrong, [sS] is better than [s|S] [sS][oO][Uu][rR][cC][eE][ ]*[iI][pP] thank you Pshemo and yes i completely forgot about escaping characters Mena thank you for pointing that out for me 编辑:我证明了我的无知,在检查regexpal后我错了,[sS]比[s | S] [sS] [oO] [Uu] [rR] [cC] [eE] [] * [iI] [更好pP]谢谢Pshemo,是的,我完全忘了逃避角色Mena谢谢你为我指出了这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM