简体   繁体   中英

Lookahead & Lookbehind including markers?

Is it possible to include the markers in a lookahead & lookbehind search?

Example:

str = "my cat is the best pet in the world"
re.findall('(?s)(?<=cat)(.*?)(?=pet)', str)

will return "is the best" what I want is "cat is the best pet"

thanks!

You should just search directly for \\bcat.*?pet\\b and forego with lookarounds:

str = "my cat is the best pet in the world"
m = re.findall(r'\bcat.*?pet\b', str)
print(m)

This prints:

['cat is the best pet']

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