Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 匹配单词';90%';使用正则表达式_Java_Regex - Fatal编程技术网

Java 匹配单词';90%';使用正则表达式

Java 匹配单词';90%';使用正则表达式,java,regex,Java,Regex,我希望单词“90%”与字符串“我拥有该公司90%的股份”匹配 如何为同一个对象编写正则表达式 我试过这样的方法: Pattern p = Pattern.compile("\\b90\\%\\b", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Matcher m = p.matcher("I have 90% shares of this company"); while (m.find()){ System.out.pri

我希望单词“90%”与字符串“我拥有该公司90%的股份”匹配

如何为同一个对象编写正则表达式

我试过这样的方法:

Pattern p = Pattern.compile("\\b90\\%\\b", Pattern.CASE_INSENSITIVE
    | Pattern.MULTILINE);
  Matcher m = p.matcher("I have 90% shares of this company");
  while (m.find()){
   System.out.println(m.group());
 }
但是没有运气

有人能在这上面点些灯吗

非常感谢,, 阿尔奇

帕伦夫妇“捕捉”了这场比赛:

/^.*(90%).*$/g

> <代码> > %> <代码>中没有<代码> \b/COD> Word边界;这就是你的模式失败的原因

请改用此模式:

"\\b90%"
另见
有三种不同的位置可以作为单词边界:

  • 在字符串的第一个字符之前,如果第一个字符是单词字符
  • 如果最后一个字符是单词字符,则在字符串中最后一个字符之后
  • 在字符串中的两个字符之间,其中一个是单词字符,另一个不是单词字符
因此,在两个字符之间,一个
\b
仅存在于一个
\W
和一个
\W
之间(按任意顺序)


'%
'
都是
\W
,所以在
“%”
中它们之间没有
\b
,我建议您格式化代码并用Java标记它。如果您只是在搜索固定字符串,那么使用正则表达式有什么意义呢?@Kenny:使用正则表达式可以指定单词边界,所以它不会匹配“990%”(我不认为这在这里经常是个问题)。/(90%)/在这种情况下是一种更简洁的方法。它匹配
99990%
(但实际上只捕获
90%
),而这似乎不是OP想要的。