简体   繁体   English

如何在正则表达式中给出积极的范围

[英]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.如果我给出re.findall(r'(?i)(?<=\b[az]{4,5})\d+', 'abcd1234 EFGHI4567')所以我需要给出 {4,5} 的范围那将是['1234','4567']我收到后视错误的错误。

How to overcome the situation如何克服这种情况

I don't think Python supports variable width lookbehinds (to my knowledge, only C# does support it).我不认为 Python 支持可变宽度后视(据我所知,只有 C# 支持它)。 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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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