Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

python - Regex that matches punctuation at the word boundary including underscore

I am looking for a Python regex for a variable phrase with the following properties: (For the sake of example, let's assume the variable phrase here is taking the value and. But note that I need to do this in a way that the thing playing the role of and can be passed in as a variable which I'll call phrase.)

Should match: this_and, this.and, (and), [and], and^, ;And, etc.

Should not match: land, andy

This is what I tried so far (where phrase is playing the role of and):

pattern = r"  " + re.escape(phrase.lower()) + r""            

This seems to work for all my requirements except that it does not match words with underscores e.g. \_hello, hello\_, hello_world.

Edit: Ideally I would like to use the standard library re module rather than any external packages.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You may use

r'(?<![^W_])and(?![^W_])'

See the regex demo. Compile with the re.I flag to enable case insensitive matching.

Details

  • (?<![^W_]) - the preceding char should not be a letter or digit char
  • and - some keyword
  • (?![^W_]) - the next char cannot be a letter or digit

Python demo:

import re
strs = ['this_and', 'this.and', '(and)', '[and]', 'and^', ';And', 'land', 'andy']
phrase = "and"
rx = re.compile(r'(?<![^W_]){}(?![^W_])'.format(re.escape(phrase)), re.I)
for s in strs:
    print("{}: {}".format(s, bool(rx.search(s))))

Output:

this_and: True
this.and: True
(and): True
[and]: True
and^: True
;And: True
land: False
andy: False

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...