Java正则表达式单词匹配 [英] Java regular expression word match

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

问题描述

我有3个值 IU,PRI RET 。如果我的输入字符串包含任何一个或多个值,则
Java正则表达式应返回true。

I have 3 values IU, PRI and RET. if my input string contains any one or more value(s),
the Java regular expression should return true.

Ex:
Values : IU PRI RET 
Input String : "put returns UI between paragraphs"

输入字符串包含UI字样,Java正则表达式应返回true。

The Input string contains "UI" word, the Java regular expression should return true.

推荐答案

您需要字词边界

boolean foundMatch = false;
Pattern regex = Pattern.compile("\\b(?:UI|PRI|RET)\\b");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();

这篇关于Java正则表达式单词匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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