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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:54:29+00:00 2026-06-12T00:54:29+00:00

Is it possible use regex to remove small words in a text? For example,

  • 0

Is it possible use regex to remove small words in a text? For example, I have the following string (text):

anytext = " in the echo chamber from Ontario duo "

I would like remove all words that is 3 characters or less. The Result should be:

"echo chamber from Ontario"

Is it possible do that using regular expression or any other python function?

Thanks.

  • 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-12T00:54:30+00:00Added an answer on June 12, 2026 at 12:54 am

    Certainly, it’s not that hard either:

    shortword = re.compile(r'\W*\b\w{1,3}\b')
    

    The above expression selects any word that is preceded by some non-word characters (essentially whitespace or the start), is between 1 and 3 characters short, and ends on a word boundary.

    >>> shortword.sub('', anytext)
    ' echo chamber from Ontario '
    

    The \b boundary matches are important here, they ensure that you don’t match just the first or last 3 characters of a word.

    The \W* at the start lets you remove both the word and the preceding non-word characters so that the rest of the sentence still matches up. Note that punctuation is included in \W, use \s if you only want to remove preceding whitespace.

    For what it’s worth, this regular expression solution preserves extra whitespace between the rest of the words, while mgilson’s version collapses multiple whitespace characters into one space. Not sure if that matters to you.

    His list comprehension solution is the faster of the two:

    >>> import timeit
    >>> def re_remove(text): return shortword.sub('', text)
    ... 
    >>> def lc_remove(text): return ' '.join(word for word in text.split() if len(word)>3)
    ... 
    >>> timeit.timeit('remove(" in the echo chamber from Ontario duo ")', 'from __main__ import re_remove as remove')
    7.0774190425872803
    >>> timeit.timeit('remove(" in the echo chamber from Ontario duo ")', 'from __main__ import lc_remove as remove')
    6.4250049591064453
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP remove special character from string I want to find a regex
Is it possible to use a regex to match February 2009, for example?
Is it possible to use regex to remove HTML tags inside a particular block
Is it possible to use stored variables in R's regex? For example I want
Possible Duplicate: How do I remove diacritics (accents) from a string in .NET? Our
Is it possible to use regex to turn this <site-ui:header title=error backURL=javascript:history.go(-1); /> into
Is it possible to use regex in spite of the newline character \n e.g.
I have a string with xml data that I pulled from a web service.
I'm trying to use regular expressions to remove certain blocks of coding from a
Is it possible to use a regex to create a pattern that matches fragments

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.