简体   繁体   English

使用正则表达式或任何其他方式匹配字符串中列表的所有单词

[英]Match all words of list in string using regex or any other way

I'm writing a python script where i have to match all words given in list with the string.我正在编写一个 python 脚本,我必须将列表中给出的所有单词与字符串匹配。 The list could be as long as possible.该列表可以尽可能长。 But i found operations that will match any of the character but couldn't find operation to match all words in list.但是我找到了匹配任何字符的操作,但找不到匹配列表中所有单词的操作。 For example例如

 s = "This is a sample string"
 list = ["is", "sample"]

// any operation such that re.search(r'',s) return correct result
// I want that regular expression or approach to do it.

Something like this?像这样的东西?

import re

string = "This is a sample string"
lst = ["is", "sample"]

for item in lst:
    rx = re.compile(r"\b{}\b".format(item))
    if rx.search(string):
        print("'{}' is in the string".format(item))

This yields这产生

'is' is in the string
'sample' is in the string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何精确匹配字符串中的所有单词,包括以 ',?.?'"' 结尾的单词,但不使用正则表达式匹配任何其他标点符号? - How do I exact match all the words in a string includes those ends with '!,?.'"' but do not match those with any other punctuation using regex? 使用正则表达式匹配列表中的单词, - Using Regex to match words in a list, 正则表达式匹配不以@开头的python列表中的所有单词 - regex match all words in a python list not preeced by @ 使用正则表达式从字符串中提取所有匹配的单词 - Extract all words that match from a string with regex 仅当另一个单词中第一个单词的索引都匹配时,如何返回一个单词与列表中所有其他单词的索引的匹配? - How match index of one word to index of all other words in list return only if all index match of first word in other any word? 以任意顺序匹配字符串中可能出现的单词列表 - Match a list of possible words in a string in any order 使用正则表达式匹配所有可能的单词组合 - Match all possible combinations of words using regex 在python中使用正则表达式匹配一行中的单词列表 - match a list of words in a line using regex in python 使用正则表达式匹配字符串中单词的最后3个字母 - Match the last 3 letters of words in a string using regex 在Python中使用regex匹配字符串中重复出现的单词 - match reoccurring words in string using regex in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM