I need to find all lines that consist only of the letters "a" and "b", and each instance of "a" immediately follows "b" and immediately precedes "b".
For instance:
mystring = 'bab babab babbab ab baba aba xyz'
Then my regex should return:
['bab' 'babab' 'babbab']
(The line "ab" - "a" is not preceded by "b". Similarly, for "aba" and "xyz" do not consist only of "a", "b".)
I used lookahead for this and wrote this regex:
re.findall(r'((?<=b)a(?=b))',mystring)
But this only returns to me all instances of "a" followed / preceded by "b":
['a','a','a','a']
But I need whole words. How can I find whole words using regular expression? I tried changing my regex with various parameters, but nothing works. How can I do that?
source share