简体   繁体   English

Python正则表达式检查字符串是否包含单词

[英]Python regex check if string contains any of words

I want to search a string and see if it contains any of the following words: AB|AG|AS|Ltd|KB|University 我想搜索一个字符串,看看它是否包含以下任何单词: AB|AG|AS|Ltd|KB|University

I have this working in javascript: 我有这个工作在JavaScript中:

var str = 'Hello test AB';
var forbiddenwords= new RegExp("AB|AG|AS|Ltd|KB|University", "g");

var matchForbidden = str.match(forbiddenwords);

if (matchForbidden !== null) {
   console.log("Contains the word");
} else {
   console.log("Does not contain the word");
}

How could I make the above work in python? 我怎样才能使上述工作在python中?

import re
strg = "Hello test AB"
#str is reserved in python, so it's better to change the variable name

forbiddenwords = re.compile('AB|AG|AS|Ltd|KB|University') 
#this is the equivalent of new RegExp('AB|AG|AS|Ltd|KB|University'), 
#returns a RegexObject object

if forbiddenwords.search(strg): print 'Contains the word'
#search returns a list of results; if the list is not empty 
#(and therefore evaluates to true), then the string contains some of the words

else: print 'Does not contain the word'
#if the list is empty (evaluates to false), string doesn't contain any of the words

You can use re module. 您可以使用re模块。 Please try below code: 请尝试以下代码:

import re
exp = re.compile('AB|AG|AS|Ltd|KB|University')
search_str = "Hello test AB"
if re.search(exp, search_str):
  print "Contains the word"
else:
  print "Does not contain the word"
str="Hello test AB"
to_match=["AB","AG","AS","Ltd","KB","University"]
for each_to_match in to_match:
    if each_to_match in str:
        print "Contains"
        break
else:
    print "doesnt contain"

You can use findall to find all matched words: 您可以使用findall查找所有匹配的单词:

import re

s= 'Hello Ltd test AB ';

find_result = re.findall(r'AB|AG|AS|Ltd|KB|University', s)

if not find_result:
    print('No words found')    
else:
    print('Words found are:', find_result)

# The result for given example s is
# Words found are: ['Ltd', 'AB']

If no word is found, that re.findall returns empty list. 如果找不到任何单词,则re.findall返回空列表。 Also its better not to use str as a name of veritable, since its overwriting build in function in python under the same name . 另外最好不要使用str作为veritable的名称,因为它会以相同的名称覆盖python中的内置函数。

暂无
暂无

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

相关问题 Python检查字符串是否包含Python中的所有单词 - Python check if string contains all words in Python 检查字符串(或拆分字符串)是否包含列表中的任何单词 - Check if string (or split string) contains any words from list Python正则表达式检查字符串是否包含方法调用 - Python regex to check if a string contains a method call 检查一个字符串是否以相同的顺序包含另一个字符串的所有单词python? - Check if a string contains all words of another string in the same order python? [Python]检查列表中的任何字符串是否包含另一个列表中的任何字符串 - [Python]Check if any string in a list is contains any string in another list Python 检查字符串是否包含来自特定字符串列表的单词 - Python check if string contains words from specific list of strings 用于检查字符串中的单词的正则表达式仅用空格分隔,而不用_AND _ / _ OR_ python分隔 - Regex to check words in string are separated only by spaces and not by _AND_/_OR_ python Python/regex:检查字符串是否仅包含数字和句点的优雅方法 - Python/regex: Elegant way to check if a string contains ONLY numbers and periods 检查字符串值的子字符串包括python中的任何字典单词 - Check substrings of a string value includes any dictionary words in python 如何检查 Python 列表是否包含任何字符串作为元素 - How to check whether a Python list contains ANY string as an element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM