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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:30:07+00:00 2026-06-05T13:30:07+00:00

In the Regular Expression engines in all languages I’m familiar with, the .* notation

  • 0

In the Regular Expression engines in all languages I’m familiar with, the .* notation indicates matching zero or more characters. Consider the following Javascript code:

var s = "baaabcccb";
var pattern = new RegExp("b.*b");
var match = pattern.exec(s);
if (match) alert(match);

This outputs baaabcccb

The same thing happens with Python:

>>> import re
>>> s = "baaabcccb"
>>> m = re.search("b.*b", s)
>>> m.group(0)
'baaabcccb'

What is the reason that both of these languages match "baaabcccb" instead of simply "baaab"? The way I read the pattern b.*b is “find a sub-string that starts with b, then has any number of other characters, then ends with b.” Both baaab and baaabcccb satisfy this requirement, yet both Javascript and Python match the latter. I would have expected it to match baaab, simply because that sub-string satisfies the requirement and appears first.

So why does the pattern match baaabcccb in this case? And, is there any way to modify this behavior (in either language) so that it matches baaab instead?

  • 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-05T13:30:09+00:00Added an answer on June 5, 2026 at 1:30 pm

    You can make the regex not greedy by adding a ? after the * like this: b.*?b. Then it will match the smallest string posible. By default the regex is greedy and will try to find the longest possible match.

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

Sidebar

Related Questions

Is there a way with a regular expression to match all characters and symbols
I need a regular expression that will not allow multiple special characters in a
I want to use a regular expression that would do the following thing (
How can I test the same regex against different regular expression engines?
(or so I think)... I'm using boost::xpressive as my regular expression engine to parse
When a regular expression is run JavaScript, is the regex engine that evaluates the
What regular expression can I use (if any) to validate that a given string
In regular expression in python [abc] matches either a or b or c How
The regular expression ^/wp-[^./]+\.php$ matches: wp-config.php wp-commentsrss2.php wp-pass.php wp-rdf.php [...] But I need another
I have a regular expression to validate the dates and it works fine, this

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.