Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:51:00+00:00 2026-05-26T08:51:00+00:00

I have a list of query terms, each with a boolean operator associated with

  • 0

I have a list of query terms, each with a boolean operator associated with them, like, say:

tom OR jerry OR desperate AND dan OR mickey AND mouse

Okay, now I have a string containing user-defined input, inputStr.

My question is, in Python, is there a way to determine if the string defined by the user contains the words in the “query”?

I have tried this:

if ('tom' or 'jerry' or 'desperate' and 'dan' or 'mickey' and 'mouse') in "cartoon dan character desperate":
    print "in string"

But it doesn’t give the output i expect.
As you can see, I don’t care about whether the query terms are ordered; just whether they are in the string or not.

Can this be done? Am I missing something like a library which can help me achieve the required functionality?

Many thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:51:01+00:00Added an answer on May 26, 2026 at 8:51 am

    To check whether any of the words in a list are in the string:

    any(word in string for word in lst)
    

    Example:

    # construct list from the query by removing 'OR', 'AND'
    query = "tom OR jerry OR desperate AND dan OR mickey AND mouse"
    lst = [term for term in query.split() if term not in ["OR", "AND"]]
    
    string = "cartoon dan character desperate"
    print any(word in string for word in lst)
    

    If you use re.search() as @jro suggested then don’t forget to escape words to avoid collisions with the regex syntax:

    import re
    m = re.search("|".join(map(re.escape, lst)), string)
    if m:
       print "some word from the list is in the string"
    

    The above code assumes that query has no meaning other than the words it contains. If it does then assuming that 'AND' binds stronger than 'OR' i.e., 'a or b and c' means 'a or (b and c)' you could check whether a string satisfies query:

    def query_in_string(query, string):
        for term in query.split('OR'):
            lst = map(str.strip, term.split('AND'))
            if all(word in string for word in lst):
               return True
        return False
    

    The above could be written more concisely but it might be less readable:

    def query_in_string(query, string):
        return any(all(word.strip() in string for word in term.split('AND'))
                   for term in query.split('OR'))
    

    Example

    query = "tom OR jerry AND dan"
    print query_in_string(query, "cartoon jerry")   # -> False no dan or tom
    print query_in_string(query, "tom is happy")    # -> True tom
    print query_in_string(query, "dan likes jerry") # -> True jerry and dan
    

    If you want to reject partial matches e.g., 'dan' should not match 'danial' then instead of word in string you
    could use re.search() and add '\b':

    re.search(r"\b%s\b" % re.escape(word), string)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which produces a list of orders like this: Invoice Description
I have list of words. I type in a word misspelled. Can I query
I have a List being returned from a daoTemplate.query() call, using a rowMapper .
I have the following LINQ query: List<string> Types = (List<string>)Directory.GetFiles(@C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) .Where(x => System.IO.Path.GetFileNameWithoutExtension(x).Contains(Microsoft)) .ToList<string>();
I currently have a list of things to update in the given mysqli query:
i have the following query to list the employees of two table. i need
I need to have a query which list all the user according to the
I have a asp.net page where i query a list of url and the
I have a query that is dynamically built after looking up a field list
I have a MySQL query in which I want to include a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.