我有3个值 IU,PRI 和 RET 。如果我的输入字符串包含任何一个或多个值,则 Java正则表达式应返回true。
Ex:
Values : IU PRI RET
Input String : "put returns UI between paragraphs"
Input字符串包含“UI”字样,Java正则表达式应返回true。
答案 0 :(得分:10)
您需要word boundaries:
boolean foundMatch = false;
Pattern regex = Pattern.compile("\\b(?:UI|PRI|RET)\\b");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
答案 1 :(得分:3)
尝试
String s= "A IU somehting PRI something RET whatever";
Pattern p= Pattern.compile("(IU|PRI|RET)");
Matcher m= p.matcher(s);
while (m.find()) {
String matched= m.group(1);
System.out.println(matched);
}
打印:
IU
PRI
RET
答案 2 :(得分:1)
我不知道您是否还在寻找解决方案。但这是你的问题的代码。我假设你要找的字谜用空格分隔,单词用大写字母表示。
String text = "put returns UI between IU paragraphs PRI RIP and RET ETR";
Pattern p = Pattern.compile("([UI]{2}|[PRI]{3}|[RET]{3})");
Matcher m = p.matcher(text);
System.out.println(m.find());
如果您尝试进行不区分大小写的匹配,请将模式更改为以下内容;
(?i)([UI]{2}|[PRI]{3}|[RET]{3})
答案 3 :(得分:0)
好的,这是一个疯狂的解决方案,每个String
都有一个字谜,内置于Pattern
只是为了好玩:
public static void main(String[] args) {
try {
Pattern pattern = makePattern("IU", "PRI", "RET");
System.out.println(pattern.pattern());
String test = "put returns UI between paragraphs, also IRP and TER";
Matcher matcher = pattern.matcher(test);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Pattern makePattern(String... words) throws Exception {
if (words == null || words.length == 0) {
throw new Exception("TODO handle invalid argument");
}
StringBuilder patternBuilder = new StringBuilder("(");
for (String word : words) {
if (word == null || word.isEmpty()) {
throw new Exception("TODO invalid word");
}
for (String anagram: doAnagrams(word, null)) {
patternBuilder.append("\\b").append(anagram).append("\\b").append("|");
}
}
patternBuilder.deleteCharAt(patternBuilder.length() - 1);
patternBuilder.append(")");
return Pattern.compile(patternBuilder.toString());
}
public static Set<String> doAnagrams(String original, Set<String> processed) {
if (original == null || original.isEmpty()) {
return new LinkedHashSet<String>();
}
Set<String> result;
if (processed == null) {
result = new LinkedHashSet<String>();
result.add(original);
} else {
result = processed;
}
if (original.length() <= 1) {
return result;
}
String sub = original.substring(1);
String subStart = original.substring(0, 1);
for (String subAnagram : doAnagrams(sub, null)) {
result.add(subAnagram.concat(subStart));
}
if (sub.concat(original.substring(0, 1)).equals(result.iterator().next())) {
return result;
}
else {
return doAnagrams(sub.concat(subStart), result);
}
}
<强>输出强>:
(\bIU\b|\bUI\b|\bPRI\b|\bRIP\b|\bIRP\b|\bIPR\b|\bPIR\b|\bRPI\b|\bRET\b|\bETR\b|\bTER\b|\bTRE\b|\bRTE\b|\bERT\b)
UI
IRP
TER
答案 4 :(得分:0)
您可以在一行中执行此操作并获取布尔值。
boolean matcher = Pattern.matches("[UI]{2}|[PRI]{3}|[RET]{3}", stringToBeMatched);