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 8996269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:43:55+00:00 2026-06-15T23:43:55+00:00

Im still learning the ropes with Python ad regular expressions and I need some

  • 0

Im still learning the ropes with Python ad regular expressions and I need some help please!
I am in need of a regular expression that can search a sentence for specific words.
I have managed to create a pattern to search for a single word but how do i retrieve the other words i need to find?
How would the re pattern look like to do this?

>>> question = "the total number of staff in 30?"
>>> re_pattern = r'\btotal.*?\b'
>>> m = re.findall(re_pattern, question)
['total']

It must look for the words “total” and “staff”
Thanks
Mike

  • 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-06-15T23:43:56+00:00Added an answer on June 15, 2026 at 11:43 pm

    Use the union operator | to search for all the words you need to find:

    In [20]: re_pattern = r'\b(?:total|staff)\b'
    
    In [21]: re.findall(re_pattern, question)
    Out[21]: ['total', 'staff']
    

    This matches your example above most closely. However, this approach only works if there are no other characters which have been prepended or appended to a word. This is often the case at the end of main and subordinate clauses in which a comma, a dot, an exclamation mark or a question mark are appended to the last word of the clause.

    For example, in the question How many people are in your staff? the approach above wouldn’t find the word staff because there is no word boundary at the end of staff. Instead, there is a question mark. But if you leave out the second \b at the end of the regular expression above, the expression would wrongly detect words in substrings, such as total in totally or totalities.

    The best way to accomplish what you want is to extract all alphanumeric characters in your sentence first and then search this list for the words you need to find:

    In [51]: def find_all_words(words, sentence):
    ....:     all_words = re.findall(r'\w+', sentence)
    ....:     words_found = []
    ....:     for word in words:
    ....:         if word in all_words:
    ....:             words_found.append(word)
    ....:     return words_found
    
    In [52]: print find_all_words(['total', 'staff'], 'The total number of staff in 30?')
    ['total', 'staff'] 
    
    In [53]: print find_all_words(['total', 'staff'], 'My staff is totally overworked.')
    ['staff']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still learning and need some help please. This code example checks the validity of
I am still learning RegEx so I need some help on this one. Rules
I'm still learning the ropes for Entity expressions, and I have this SQL query
... and thanks for reading... I'm still learning the ropes so please be forgiving...
I'm still learning CSS, so please pardon me if this is something that is
I am still learning Perl scripting and need help in doing below task. Sample
Still learning regular expression! My percentage regular expression (not respecting the 100 limit): ^(?<int>[1-9][0-9]*|[0])(?<dec>\.[0-9]+)?\%?$
I'm still learning the ropes of Git (love it!) but the other day I
im not the best when it comes to SQL still learning the ropes. I
Still learning magento coding. I wonder is there a way I can print out

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.