Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I got the following scenarios:

1) car on the right shoulder
2) car on the left shoulder
3) car on the shoulder

I want to match "shoulder" when left|right is not present. So only 3) return "shoulder"

re.compile(r'(?<!right|rights*)shoulder')
sre_constants.error: look-behind requires fixed-width pattern

It seems like I can't use s* and "|"

How can I solve this.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
683 views
Welcome To Ask or Share your Answers For Others

1 Answer

regex module: variable-width lookbehind

In addition to the answer by HamZa, for any regex of any complexity in Python, I recommend using the outstanding regex module by Matthew Barnett. It supports infinite lookbehind—one of the few engines to do so, along with .NET and JGSoft.

This allows you to do for instance:

import regex
if regex.search("(?<!right |left )shoulder", "left shoulder"):
    print("It matches!")
else:
    print("Nah... No match.")

You could also use s+ if you wished.

Output:

It matches!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...