Java Regex:将整个单词与单词边界匹配 [英] Java Regex : match whole word with word boundary

查看:64
本文介绍了Java Regex:将整个单词与单词边界匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java检查字符串是否整体上包含单词.下面是一些示例:

I am trying to check whether a string contains a word as a whole, using Java. Below are some examples:

Text : "A quick brown fox"
Words:
"qui" - false
"quick" - true
"quick brown" - true
"ox" - false
"A" - true

下面是我的代码:

String pattern = "\\b(<word>)\\b";
String s = "ox";
String text = "A quick brown fox".toLowerCase();
System.out.println(Pattern.compile(pattern.replaceAll("<word>", s.toLowerCase())).matcher(text).find());

与上面示例中提到的字符串一样,它也可以正常工作.但是,如果输入字符串包含(等)等字符,我会得到错误的结果,例如:

It works fine with strings like the one I mentioned in the above example. However, I get incorrect results if the input string has characters like %, ( etc, e.g.:

Text : "c14, 50%; something (in) bracket"
Words:
"c14, 50%;" : false
"(in) bracket" : false

它与我的 regex 模式有关(或者我在错误地匹配整个模式).谁能建议我一个更好的方法.

It has something to do with my regex pattern (or maybe I am doing the entire pattern matching wrongly). Could anyone suggest me a better approach.

推荐答案

似乎您只想匹配用空格括起来的单词"(或在字符串的开头/结尾).

It appears you only want to match "words" enclosed with whitespace (or at the start/end of strings).

使用

String pattern = "(?<!\\S)" + Pattern.quote(word) + "(?!\\S)";

后面的(?<!\ S)否定性查找将使所有前面带有除空格和(?!\ s)以外的字符的所有匹配项失败.是一个否定的超前查询,将使所有紧随其后的字符(除空格以外)都失败. Pattern.quote()是转义特殊字符的必要条件,这些特殊字符需要在正则表达式模式中视为文字字符.

The (?<!\S) negative lookbehind will fail all matches that are immediately preceded with a char other than a whitespace and (?!\s) is a negative lookahead that will fail all matches that are immediately followed with a char other than whitespace. Pattern.quote() is necessary to escape special chars that need to be treated as literal chars in the regex pattern.

这篇关于Java Regex:将整个单词与单词边界匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆