I can’t understand how to solve the following problem:
I have input string “aaaabaa” and I’m trying to search for string “aa” (I’m looking for positions of characters)
Expected result is
0 1 2 5
- aa aabaa
- a aa abaa
- aa aa baa
- aaaab aa
This problem is already solved by me using another approach (non-RegEx).
But I need a RegEx I’m new to RegEx so google-search can’t help me really.
Any help appreciated! Thanks!
P.S.
I’ve tried to use (aa)* and "\b(\w+(aa))*\w+" but those expressions are wrong
You can solve this by using a lookahead
will find every “a” that is followed by another “a”.
If you want to do this more generally
This will find every character that is followed by the same character. Every found letter is stored in a capturing group (because of the brackets around), this capturing group is then reused by the positive lookahead assertion (the
(?=...)) by using\1(in\1there is the matches character stored)\p{L}is a unicode code point with the category “letter”Code
Output