简体   繁体   中英

python regex: match a char surrounded by exactly 2 chars

I need a regex in python that matches any char that is surrounded by exactly 2 underscores. meaning, meaning

__a__ 

will match "a", but

___a___ 

will not match. it needs to support overlapping matches, such that

__a__d___b___e__c__ 

will return "ac" because a is surrounded by double underlines, but d,e have a triple one next to them and b has a triple underline on both sides. what I have now

(?<=[_]{2})(.)(?=[_]{2})

it solves the overlapping, but not the "exactly 2" in the above example it returns "adbec"

Try the following:

(?<=(?<!_)__)([^_])(?=__(?!_))

Examples:

>>> regex = re.compile(r'(?<=(?<!_)__)([^_])(?=__(?!_))')
>>> regex.findall('__a__ ')
['a']
>>> regex.findall('___a___')
[]
>>> regex.findall('__a__d___b___e__c__')
['a', 'c']
>>> regex.findall('__a__c__')
['a', 'c']

You said you wanted overlapping matches, but if you would not like the c to match in __a__c__ , use the following (which was my original answer):

(?<!_)__([^_])__(?!_)

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