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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:17:55+00:00 2026-05-27T03:17:55+00:00

I have to match all the alphanumeric words from a text. >>> import re

  • 0

I have to match all the alphanumeric words from a text.

>>> import re
>>> text = "hello world!! how are you?"
>>> final_list = re.findall(r"[a-zA-Z0-9]+", text)
>>> final_list
['hello', 'world', 'how', 'are', 'you']
>>>

This is fine, but further I have few words to negate i.e. the words that shouldn’t be in my final list.

>>> negate_words = ['world', 'other', 'words']

A bad way to do it

>>> negate_str = '|'.join(negate_words)
>>> filter(lambda x: not re.match(negate_str, x), final_list)
['hello', 'how', 'are', 'you']

But i can save a loop if my very first regex-pattern can be changed to consider negation of those words. I found negation of characters but i have words to negate, also i found regex-lookbehind in other questions, but that doesn’t help either.

Can it be done using python re?

Update

My text can span a few hundered lines. Also, list of negate_words can be lengthy too.

Considering this, is using regex for such task, correct in the first place?? Any suggestions??

  • 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-27T03:17:56+00:00Added an answer on May 27, 2026 at 3:17 am

    I don’t think there is a clean way to do this using regular expressions. The closest I could find was bit ugly and not exactly what you wanted:

    >>> re.findall(r"\b(?:world|other|words)|([a-zA-Z0-9]+)\b", text)
    ['hello', '', 'how', 'are', 'you']
    

    Why not use Python’s sets instead. They are very fast:

    >>> list(set(final_list) - set(negate_words))
    ['hello', 'how', 'are', 'you']
    

    If order is important, see the reply from @glglgl below. His list comprehension version is very readable. Here’s a fast but less readable equivalent using itertools:

    >>> negate_words_set = set(negate_words)
    >>> list(itertools.ifilterfalse(negate_words_set.__contains__, final_list))
    ['hello', 'how', 'are', 'you']
    

    Another alternative is the build-up the word list in a single pass using re.finditer:

    >>> result = []
    >>> negate_words_set = set(negate_words)
    >>> result = []
    >>> for mo in re.finditer(r"[a-zA-Z0-9]+", text):
        word = mo.group()
        if word not in negate_words_set:
             result.append(word)
    
    >>> result
    ['hello', 'how', 'are', 'you']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to match all lines that have any uppercase characters in them but
guys i have arrays in which i have to match this kind of text
How do I match all child nodes containing text recursively. If I have a
I have the following Regex to match all link tags on a page generated
I have a problem. I'd like to match all occurrences of \t in my
I have two LinkedLists, I'd like to move all elements which match some criteria
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
I need to match all urls that DO have mydomain.com in them AND DO
I have problem with the following perl command How to match all 1234 strings
How can I match all a-tags which have a font-size of 70%? <a href=/tag/customersproducts

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.