Fundamentals 5 min read

Detect Multiple Keywords in a Python String with any(), Regex, and Custom Functions

This article explains three practical methods—using the any() function, regular expressions, and a custom function—to check whether a Python string contains any of several specified keywords, complete with code examples and a concise summary.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Detect Multiple Keywords in a Python String with any(), Regex, and Custom Functions

Introduction

In a recent Python community discussion, a user asked how to determine if a string contains any of several keywords such as "宿舍", "公寓", or "酒店" and return 1 if a match is found.

Solution Overview

Several contributors suggested different approaches, including using a list comprehension with any(), regular expressions, and a custom function that returns 1 upon a match. Below are three consolidated implementations.

Method 1: any() Function

This method leverages any() with a list comprehension to check for the presence of any keyword.

s = '宿舍 饿了 酒店'
any([x in s for x in ['宿舍', '公寓', '酒店']])

The expression evaluates to True if any keyword is found.

Method 2: Regular Expression

Using the re module, we can search for any of the keywords with a single pattern.

import re
text = '宿舍 饿了 酒店'
re.search('宿舍|公寓|酒店', text)

The search function returns a match object when a keyword is present.

Method 3: Custom Function Returning 1

A custom function iterates over the keyword list and returns 1 as soon as a match is found.

# coding: utf-8
import re

def find_kw(text):
    kw = ['宿舍', '公寓', '酒店']
    for k in kw:
        f_t = re.search(k, text)  # Returns a match object or None
        if f_t:
            return 1

if __name__ == '__main__':
    text = '我住在希尔顿酒店'
    result = find_kw(text)
    if result:
        print(result)  # Prints 1 if a keyword is found

This approach can be easily adapted to return other values or perform additional actions.

Conclusion

The three methods demonstrate flexible ways to solve the keyword‑search problem in Python. Depending on the specific requirements—whether you need a boolean result, a match object, or a custom return value—you can choose the most suitable implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

anystring searchcode-example
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.