python - 正则表达式匹配单词,但仅当它不以非字母数字字符开头时

标签 python regex string

我想要识别句子中的单词,但如果它以字母数字字符开头则不需要。如果以 1 结尾也没关系。

我所做的一个例子;

words = ["THIS", "THAT"]
sentences = ["I want to identify THIS word.", "And THAT!", "But I do not want to identify !THIS word", "Or [THIS] word"] 

for sentence in sentences:
        for word in words:
                word_re = re.search(r"\b(%s)\b" %word, sentence) 
                if word_re:
                    print("It's a match!")

上面代码的输出将在每个句子中匹配。我想要一些只匹配前两句话的东西。 可以用正则表达式做我想做的事吗?

谢谢!

最佳答案

您可以使用正则表达式,例如

(?<!\S)(?:THIS|THAT)\b

请参阅regex demo详细信息:

  • (?<!\S) - 左侧空白边界
  • (?:THIS|THAT) - 匹配 THIS 的非捕获组或THAT
  • \b - 单词边界。

请参阅Python demo :

import re
words = ["THIS", "THAT"]
sentences = ["I want to identify THIS word.", "And THAT!", "But I do not want to identify !THIS word", "Or [THIS] word"] 

pattern = fr"(?<!\S)(?:{'|'.join(words)})\b"
for sentence in sentences:
    word_re = re.search(pattern, sentence) 
    if word_re:
        print(f"'{sentence}' is a match!")

# => 'I want to identify THIS word.' is a match!
#    'And THAT!' is a match!

如果THISTHAT可以包含特殊字符,替换 pattern = fr"(?<!\S)(?:{'|'.join(words)})\b"pattern = fr"(?<!\S)(?:{'|'.join(map(re.escape, words))})\b" .

关于python - 正则表达式匹配单词,但仅当它不以非字母数字字符开头时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67289995/

相关文章:

regex - OpenCart SEO index.php .htaccess 重定向

regex - 匹配正则表达式模式后打印行

c++ - 使用 C++ fmtlib,是否有比使用 std::ostringstream 更简洁的方法将数据序列 append 到字符串?

python - 在python中解析一个字符串直到一个空格以形成一个列表

java - 有哪些 Java 库提供了从给定字符集生成唯一随机字符串组合的功能?

python - 在 Django 中更改图像类型

python - 在 matplotlib 中使用循环变量指定颜色

python - 如何使用 Selenium 和 Python 在网站 https ://www. virustotal.com 中找到 shadow-root(打开)中的名字字段

python - 如何根据单独数据帧中列值的存在来过滤数据帧的行并附加第二个数据帧中的列

javascript - JS 正则表达式限制