开发者

Regex: how to match overlapping patterns (maybe Python specific)

开发者 https://www.devze.com 2023-02-14 13:53 出处:网络
I have a string that looks like this: \"XaXbXcX\". I\'m looking to match any lowercase letters surrounded by X on either side. I tried this in Python, but I\'m not getting what I\'m looking for:

I have a string that looks like this: "XaXbXcX". I'm looking to match any lowercase letters surrounded by X on either side. I tried this in Python, but I'm not getting what I'm looking for:

import re
str = "XaXbXcX"
pattern = r'X([a-z])X'
matches = re.f开发者_如何学Cindall(pattern, str) # gives me ['a', 'c']. What about b?


You can use a lookbehind assertion:

pattern = r'(?<=X)([a-z])X'


I do not know python, however this regex works i tested in gskinner too ([^(?:X)+])+.

Hope this helps you

0

精彩评论

暂无评论...
验证码 换一张
取 消