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.
Try
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);
}
It prints:
IU
PRI
RET
Ok here's a crazy solution with anagrams of each given String
, built into a Pattern
just for fun:
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);
}
}
Output:
(\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
You need word boundaries for that:
boolean foundMatch = false;
Pattern regex = Pattern.compile("\\b(?:UI|PRI|RET)\\b");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
I don't know if you are still looking for the solution of this. But here's the code for your question. I assumed that the anagrams you are looking for are separated by spaces and the words appear in Uppercase.
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());
If you are try for case insensitive matching, change the pattern to the following;
(?i)([UI]{2}|[PRI]{3}|[RET]{3})
You could do this in one line and get your boolean value.
boolean matcher = Pattern.matches("[UI]{2}|[PRI]{3}|[RET]{3}", stringToBeMatched);