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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:03:35+00:00 2026-06-15T04:03:35+00:00

I am making several regex substitutions in Python along the lines of \w\s+\w over

  • 0

I am making several regex substitutions in Python along the lines of

  \w\s+\w  

over many large documents. Obviously if I make the regex non-greedy (with a ?) it won’t change what it matches (as \w != \s) but will it make the code run any faster?
In other words, with non-greedy regexes does Python work its way from the first character matched onwards rather than from the end of the document back to that character, or is this a naive view?

  • 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-15T04:03:37+00:00Added an answer on June 15, 2026 at 4:03 am

    Is this the pattern you implied?

    In [15]: s = 'some text   with \tspaces  between'
    
    In [16]: timeit re.sub(r'(\w)(\s+)(\w)', '\\1 \\3', s)
    10000 loops, best of 3: 30.5 us per loop
    
    In [17]: timeit re.sub(r'(\w)(\s+?)(\w)', '\\1 \\3', s)
    10000 loops, best of 3: 24.9 us per loop
    

    Seems to be a pretty small difference here. Only 5 microseconds with the non-greedy,

    Using a 500 word lorem-ipsum, with multiple mixed whitespace between every word, I get an 8 ms difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several large CSS files and making a change can sometimes take a
I've a Java client which accesses our server side over HTTP making several small
I am making several http requests to a particular host using python's urllib2 library.
I have several columns and making pivot. I want to have multiple non-pivot columns
My coworker suggested making several of the Eclipse code-formatting and warning settings to be
The program I am making has several views in which the user is trying
I've got a table with several columns making up the primary key. The nature
I am making an android application which has a main activity and several other
My project uses several dll files with activex controls. Now I am making a
I'm making a 2D map-based game. The player controls one character while several AI-controlled

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.