Home  >  Q&A  >  body text

python - In regular expressions, if the second half of the matched string happens to be the first half of the next matched string, how to implement it?

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 now aAFDdADDdDFDsDFS, 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!

给我你的怀抱给我你的怀抱2686 days ago922

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-12 09:29:43

    r'(?<=[A-Z]{3})([a-z])(?=[A-Z]{3})'

    >>> import re
    >>> rawstring = 'aAFDdADDdDFDsDFS'
    >>> reg = r'(?<=[A-Z]{3})([a-z])(?=[A-Z]{3})'
    >>> pattern = re.compile(reg)
    >>> pattern.findall(rawstring)
    ['d', 'd', 's']

    reply
    0
  • Cancelreply