简体   繁体   中英

How to give range in positive look behind in regex

import re
re.findall(r'(?i)(?<=\b[a-z]{4})\d+', 'abcd1234  EFGHI4567')

My Out

['1234']

I need to give range of {4,5} if i am giving re.findall(r'(?i)(?<=\b[az]{4,5})\d+', 'abcd1234 EFGHI4567') so that out will be ['1234','4567'] I am getting error of look behind error.

How to overcome the situation

I don't think Python supports variable width lookbehinds (to my knowledge, only C# does support it). I would rephrase your regex search without lookbehinds as:

inp = "abcd1234 bla123 EFGHI4567"
matches = re.findall(r'\b[A-Za-z]{4,5}(\d+)\b', inp)
print(matches)

This prints:

['1234', '4567']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM