I was reviewing the re module recently, and suddenly I thought of this question, that is, if the second half of the matched string happens to be the first half of the next matched string, how to achieve it? For example, there is a string nowaAFDdADDdDFDsDFS
, and I want to match the lowercase letters d, d and s surrounded by three uppercase letters. My code is as follows:
import re rawstring = 'aAFDdADDdDFDsDFS' reg = r'[^A-Z]*[A-Z]{3}([a-z]+)[A-Z]{3}[^A-Z]*' pattern = re.compile(reg) r = pattern.finditer(rawstring) for s in r: print(s.group())
The results obtained are as follows:
aAFDdADDd DFDsDFS
The second d is missing. What should I do if I want to match the second d as well? Thanks!
r'(?<=[A-Z]{3})([a-z])(?=[A-Z]{3})'