简体   繁体   中英

Extracting string with an optional ending pattern

I want to extract a substring that may either appear between two substrings or in the end of the original string. The starting delimiter is ab and the ending delimiter may be cd or the end of the original string.

Examples:

c = 'ab123:random text1 cd4576:text2'
d = 'cd123:text2 ab75589:text1'
e = 'ab35:rand text2 cd765:text1'

Desired answer:

c = 'random text1'
d = 'text1'
e = 'rand text2'

I am able to match the starting substring with re.findall('ab\\d+:(.*)', i) . But when I try to add the ending pattern, I cannot find the desired answer:

re.findall('ab\d+:(.*)', i)
>>> ['random text1 cd4576: text2'], [' text1'], ['rand text2 cd765: text1']

re.findall('^ab\d+:(.*)cd\d+:', i)
>>>['random text1 '], [], ['rand text2 ']

您可以使用re.findall(r'\\bab\\d+:(.*?)(?:\\s*\\bcd|$)', i)代替。

try using the or "|" with group like this:

re.findall('ab[^:]+:[ \t]*(.+)[ \t]*(cd[^:]+|$):', i)

you also neet to exclude the "cd" inside content itself (in this pattern, a space serve as separator but imagine variant on string like 'ab123:random text1 de23:acdc cd4576:text2'

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