正则表达式在java中查找字符串中的特定单词 [英] Regex to find a specific word in a string in java

查看:147
本文介绍了正则表达式在java中查找字符串中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些正则表达式的帮助:我正在尝试检查句子是否包含特定单词。

I need some help with regular expressions: I'm trying to check if a sentence contains a specific word.

让我们以本主题的标题为例:

let's take for example the title of this topic:

正则表达式在字符串中查找特定单词

"Regex to find a specific word in a string"

我需要查找它是否包含单词 if ,在这种情况下它是假的。

I need to find if it contains the word if, which in this case it's false.

我不能使用方法contains,因为在这种情况下它会返回true(spec * if * ic)

I can't use the method contains because it would return true in this case (spec*if*ic)

我在考虑使用方法匹配,但我对正则表达式有点不同。

I was thinking about using the method matches but I'm kinda noob with regular expressions.

基本上,匹配方法的输入中的正则表达式需要指定正在查找的单词之前和单词之后的字符不是字母(因此它不能包含在那个词)或者这个词在句子的开头或结尾处

Basically the regex in input to the matched method needs to specify that the character right before the word I'm looking for and right after the word is not alphabetic (so it couldn't be contained in that word) or that the word is at the beginning or at the end of the sentence

非常感谢!

推荐答案

使用以下正则表达式:

".*\\bif\\b.*"

\ b 匹配字边界。

这篇关于正则表达式在java中查找字符串中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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