If you want to match any string that contains two or more e's, anywhere in the string, this regex would do it:
.*e.*e.*
But extending this to look for other characters, that could be anywhere in the string relative to those two e's, isn't trivial. You can do it with a lookahead:
(?=.*l)(?=.*a).*e.*e.*
which matches if, at some point in the source string, the matcher can find all of the patterns .*l
, .*a
, and .*e.*e.*
starting from that point. This will tell if there's one l, one a, and at least two e's, in any order. (If you want to look for, say, two l's, then something like this will work:)
(?=.*l.*l)(?=.*a).*e.*e.*
However, this requires that the matcher perform three scans starting at each point in the regex. That's why I believe problems like this are better solved with a loop:
eCount = 0;
aCount = 0;
lCount = 0;
for (int i = 0; i < s.length(); i++) {
switch(s.charAt(i)) {
case 'e':
eCount++;
break;
case 'a':
aCount++;
break;
case 'l':
lCount++;
break;
}
}
if (eCount >= 2 && aCount >= 1 && lCount >= 1) { ... }
More verbose, but overall I think more efficient.