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

  • Home
  • SEARCH
  • 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 6176299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:04:34+00:00 2026-05-24T00:04:34+00:00

I have a list of strings in which I want to filter for strings

  • 0

I have a list of strings in which I want to filter for strings that contains keywords.

I want to do something like:

fruit = re.compile('apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi']

so I can then use re.search(fruit, list_of_strings) to get only the strings containing fruits, but I’m not sure how to use a list with re.compile. Any suggestions? (I’m not set on using re.compile, but I think regular expressions would be a good way to do this.)

  • 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-24T00:04:35+00:00Added an answer on May 24, 2026 at 12:04 am

    You need to turn your fruit list into the string apple|banana|peach|plum|pineapple|kiwi so that it is a valid regex. The following should do this for you:

    fruit_list = ['apple', 'banana', 'peach', 'plum', 'pineapple', 'kiwi']
    fruit = re.compile('|'.join(fruit_list))
    

    As ridgerunner pointed out in comments, you will probably want to add word boundaries to the regex, otherwise the regex will match on words like plump since they have a fruit as a substring.

    fruit = re.compile(r'\b(?:%s)\b' % '|'.join(fruit_list))
    

    Lastly, if the strings in fruit_list could contain special characters, you will probably want to use re.escape.

    '|'.join(map(re.escape, fruit_list))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of strings that is regenerated every 5 seconds. I want
I have a list of items(i.e Strings) which I need to sort/filter. The end
I have a list of strings which I paint on an axis. I want
I have a ListView which displays a list of string values. I want to
Example of the problem If I have a list of valid option strings which
I have a simple list of strings, which may be of arbitrary length. I'd
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
I have a list which contains some items of type string. List<string> lstOriginal; I
I have a list of strings and a list of filters (which are also
I have a list of Person s each which have multiple fields that I

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.