Well, it's really simple, we have the following line
aaa aaa bbb
Let's see that we have this regular expression aaa.*?bbb . Regex engine starts with aaa
aaa aaa bbb
Now the regex engine has .*?bbb . He will continue to work with space
aaa space aaa bbb
but do we still have characters before bbb ? Thus, the regex engine will continue its path and match the second set
aaa aaa space bbb
Finally, the regex engine will match bbb :
aaa aaa bbb
So, let's see if we only want to combine the second aaa , we can use the following regular expression:
(?<!^)aaa.*?bbb , which means that it matches aaa , which is not at the beginning of the sentence.
We can also use aaa(?= bbb).*?bbb , which means that space bbb follows to match aaa .
See how 1 - 2 works.
Just come round, but why don't you use aaa bbb ?
Hamza
source share