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):
(?<!_)__([^_])__(?!_)