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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:21+00:00 2026-05-27T07:36:21+00:00

I need to preprocess tweets using Python. Now I am wondering what would be

  • 0

I need to preprocess tweets using Python. Now I am wondering what would be the regular expression to remove all the hashtags, @user and links of a tweet respectively?

for example,

  1. original tweet: @peter I really love that shirt at #Macy. http://bet.ly//WjdiW4
    • processed tweet: I really love that shirt at Macy

  2. original tweet: @shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bet.ly/tuN2wx

    • processed tweet: Titanic tragedy could have been prevented Economic Times Telegraph co ukTitanic tragedy could have been preve
  3. original tweet: I am at Starbucks http://4sh.com/samqUI (7419 3rd ave, at 75th, Brooklyn)
    • processed tweet: I am at Starbucks 7419 3rd ave at 75th Brooklyn

I just need the meaningful words in each Tweet. I don’t need the username, or any links or any punctuations.

  • 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-27T07:36:22+00:00Added an answer on May 27, 2026 at 7:36 am

    The following example is a close approximation. Unfortunately there is no right way to do it just via regular expression. The following regex just strips of an URL (not just http), any punctuations, User Names or Any non alphanumeric characters. It also separates the word with a single space. If you want to parse the tweet as you are intending you need more intelligence in the system. Some precognitive self learning algorithm considering there is no standard tweet feed format.

    Here is what I am proposing.

    ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    

    and here is the result on your examples

    >>> x="@peter I really love that shirt at #Macy. http://bit.ly//WjdiW4"
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'I really love that shirt at Macy'
    >>> x="@shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bit.ly/tuN2wx"
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'Titanic tragedy could have been prevented Economic Times Telegraph co ukTitanic tragedy could have been preve'
    >>> x="I am at Starbucks http://4sq.com/samqUI (7419 3rd ave, at 75th, Brooklyn) "
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'I am at Starbucks 7419 3rd ave at 75th Brooklyn'
    >>> 
    

    and here are few examples where it is not perfect

    >>> x="I c RT @iamFink: @SamanthaSpice that's my excited face and my regular face. The expression never changes."
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'I c RT that s my excited face and my regular face The expression never changes'
    >>> x="RT @AstrologyForYou: #Gemini recharges through regular contact with people of like mind, and social involvement that allows expression of their ideas"
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'RT Gemini recharges through regular contact with people of like mind and social involvement that allows expression of their ideas'
    >>> # Though after you add # to the regex expression filter, results become a bit better
    >>> ' '.join(re.sub("([@#][A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'RT recharges through regular contact with people of like mind and social involvement that allows expression of their ideas'
    >>> x="New comment by diego.bosca: Re: Re: wrong regular expression? http://t.co/4KOb94ua"
    >>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    'New comment by diego bosca Re Re wrong regular expression'
    >>> #See how miserably it performed?
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to match C++ preprocessor statements. Now, preprocessor statements may span multiple lines:
I need to mimic the preprocessor feature of C with Python. If I want
Need to an expression that returns only things with an I followed by either
I have rebuilt a project. I've migrated databases and now I need to transfer
I need to make a lot of operations using BigDecimal, and I found having
I need to pull user profile fields to display in my page.tpl. I know
I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
what I need is to match multiline preprocessor's statements such as: #define max(a,b) \
Need a function that takes a character as a parameter and returns true if
Need a way to allow sorting except for last item with in a list.

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.