Reputation: 79
Is it possible to write regex in Java that matches word parts? For example I want to search for strings 'ab', 'cd' and 'ef'. A match should be returned in following examples:
[lab stef ecde], [lecde effe aaaab]
So a match should be returned if all strings ('ab', 'cd', 'ef') are parts of words anywhere in the text - order is not imported. However match shouldn't be returned if any of strings are missing
[lab stef]
Upvotes: 0
Views: 199
Reputation: 124275
If it doesn't have to be regex then Tichodroma's answer is the one you are looking for.
But if you really need to complicate your life and use regex you can try to use look-around mechanisms like look ahead and create something like
"lab stef ecde".matches("(?=.*ab)(?=.*cd)(?=.*ef).*") //true
"lab stef".matches("(?=.*ab)(?=.*cd)(?=.*ef).*") //false
to explain it more clearly: in
(?=.*ab)(?=.*cd)(?=.*ef).*
(?=.*ab)
will check if your string contains .*ab
where .*
will match any characters before ab
part. (?=.*cd)
and (?=.*ef)
.*
at the end, because matches
check if entire string matches our regex, so we need to somehow iterate over entire string. Upvotes: 6
Reputation: 878
This will do:
^.*(ab.*cd.*ef|ab.*ef.*cd|cd.*ab.*ef|cd.*ef.*ab|ef.*ab.*cd|ef.*cd.*ab).*$
You can test it here: http://www.regexplanet.com/advanced/java/index.html
I believe it is an overkill though. Another optimized solution would be better.
Upvotes: 0
Reputation:
Find every substring in the input and &&
the resulting boolean
values.
String s = "lab stef ecde";
boolean ab = s.indexOf("ab") > -1;
boolean cd = s.indexOf("cd") > -1;
boolean ef = s.indexOf("ef") > -1;
boolean match = ab && cd && ef; // true
Edit
In Germany there is a proverb:
Warum einfach wenn es auch kompliziert geht?
"Why simple when you can do it complicated?"
That's what I think about regular expressions in this case.
Upvotes: 5