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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:52:01+00:00 2026-06-18T03:52:01+00:00

What does this Python regex match? .*?[^\\]\n I’m confused about why the . is

  • 0

What does this Python regex match?

.*?[^\\]\n

I’m confused about why the . is followed by both * and ?.

  • 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-18T03:52:02+00:00Added an answer on June 18, 2026 at 3:52 am

    * means “match the previous element as many times as possible (zero or more times)”.

    *? means “match the previous element as few times as possible (zero or more times)”.

    The other answers already address this, but what they don’t bring up is how it changes the regex, well if the re.DOTALL flag is provided it makes a huge difference, because . will match line break characters with that enabled. So .*[^\\]\n would match from the beginning of the string all the way to the last newline character that is not preceeded by a backslash (so several lines would match).

    If the re.DOTALL flag is not provided, the difference is more subtle, [^\\] will match everything other than backslash, including line break characters. Consider the following example:

    >>> import re
    >>> s = "foo\n\nbar"
    >>> re.findall(r'.*?[^\\]\n', s)
    ['foo\n']
    >>> re.findall(r'.*[^\\]\n', s)
    ['foo\n\n']
    

    So the purpose of this regex is to find non-empty lines that don’t end with a backslash, but if you use .* instead of .*? you will match an extra \n if you have an empty line following a non-empty line.

    This happens because .*? will only match fo, [^\\] will match the second o, and the the \n matches at the end of the first line. However the .* will match foo, the [^\\] will match the \n to end the first line, and the next \n will match because the second line is blank.

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

Sidebar

Related Questions

Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
I have a Python program (with Django - does this matter?) that I want
I'm newbie in Python. I can't understand why this code does not work: reOptions
Does python support random json serialization? I get this error: Traceback (most recent call
(I'm sure this is a FAQ, but also hard to google) Why does Python
Does this code always evaluate to false? Both variables are two's complement signed ints.
i am working on a regex match function in python. i have the following
This question might seem vague, sorry. Does anybody have experience writing RegEx with Objective-C
In Python compiled regex patterns have a findall method that does the following: Return
Does this function has the same behavior that memset ? inline void SetZeroArray( void

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.