-2

How would one write a regex to match this:

Regex matching all words containing exactly two letter e's and between 1 and 3 letter a's

I have no idea where to even start, my thoughts would be to use lookahead; but, how do I apply lookahead twice to this problem? Is that even possible?

Sam
  • 18,756
  • 2
  • 40
  • 65
Display
  • 216
  • 1
  • 13
  • 4
    Start at [Learning Regular Expressions](http://stackoverflow.com/a/2759417/3832970). Then, do not forget to check [How to ask](http://stackoverflow.com/help/how-to-ask). – Wiktor Stribiżew Jan 19 '16 at 21:02
  • it would be easier to use a 2-pass flow: first find all the ones with `e`s, then filter() all those for `a`s. – dandavis Jan 19 '16 at 21:03
  • Your link is not very helpful Wiktor, I am familiar with regular expression syntax in its basics. dandavis, is it possible to solve this just writing one regular expression? I would have approached it using that technique too but say there is an acamdemic constraint to just use one regular expression for it – Display Jan 19 '16 at 21:07
  • 2
    So, @Display, what have you tried? Give it a shot in a [regex tester](http://regex101.com/) and then we can help you from there. – Sam Jan 19 '16 at 21:14
  • hint: `(?=TEST1)(?=TEST2).+` – Bart Kiers Jan 19 '16 at 21:22

1 Answers1

2

Your hunch to use lookahead twice is good. Here is a solution:

((?=\b(?:[a-df-z0-9_]*e[a-df-z0-9_]*){2}\b)(?=\b(?:[b-z0-9_]*a[b-z0-9_]*){1,3}\b).+)

Here is the same regex with token explanation on regex101.

Talmid
  • 963
  • 10
  • 16