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();