Java Regex 查找单词的完全匹配项 [英] Java Regex to find a exact match for a word

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

问题描述

我正在尝试在 Java 中构建一个正则表达式模式来查找单词的完全匹配.

I am trying to build a regex pattern in Java to find a exact match of a word.

例如,单词 hot 应该出现在前 3 个字符串中,而不出现在第四个字符串中.

For example, the word hot should be found in the first 3 strings but not in the fourth one.

hot in here
It's hot
how hot is it
a@hotmail.com --- > should not match.

我尝试使用以下正则表达式,但它不起作用.

I tried with the following regex but it does not work.

/(?: hot )/g

正确的模式是什么?

推荐答案

您可以在模式周围使用字边界 \b.词边界 不消耗任何字符.它断言一侧有一个单词字符,而另一侧没有.

You can use word boundaries \b around your pattern. A word boundary does not consume any characters. It asserts that on one side there is a word character, and on the other side there is not.

\bhot\b

这篇关于Java Regex 查找单词的完全匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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