开发者

Java regex pattern to match word or phrase

I have the following regular expression, that I am compiling with Pattern class.

\bIntegrated\s+Health\s+System\s+\(IHS\)\b

Why is this not matching this string?

"test pattern case Integrated Health System (IHS)."

If I try \bpattern\b, it seems to work, but for the above phrase it does not. I have the parenthesis in the pattern escaped, so not开发者_运维百科 sure why it doesn't work. It does match if I remove the parenthesis portion of the pattern, but I want to match the whole thing.


1) escape the parens, otherwise they are capturing and group metacharacters, not literal parenthesis \( \)

2) remove the final \b you can't use a word boundary after a literal ), since ) is not considered part of a word.

\bIntegrated\s+Health\s+System\s+\(IHS\)\W


You've got (IHS) - a group - where you want \(IHS\) as the literal brackets.


You need to escape the parentheses

\bIntegrated\s+Health\s+System\s+\(IHS\)\b

Parentheses delimit a capture group. To match a literal set of parentheses, you can escape them like this \( \)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜