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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:30:27+00:00 2026-05-15T08:30:27+00:00

I have the following regex: a?\W*?b and I have a string ,.! ,b When

  • 0

I have the following regex: a?\W*?b
and I have a string ,.! ,b
When searching for a match I get ,.! ,b, but not just b as I expect. Why is that? How to modify the regex to get what I need?
Thank you for your help.

  • 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-15T08:30:28+00:00Added an answer on May 15, 2026 at 8:30 am

    A lazy quantifier doesn’t help here for what you want. Let’s see what’s happening.

    The regex engine starts at the beginning of the string. First tries to match a. It can’t, but it’s no problem since the a is optional.

    Then, there is a lazy \W*? so the regex engine skips it but remembers the current position.

    It then tries to match b. It can’t, so it backtracks and successfully matches the , with \W*?. It then goes on to try and match b (because of the lazy quantifier). It still can’t and backtracks again. This repeats a few times until finally the regex engine has arrived at the b. Now the match is complete – the regex engine declares success.

    So the regex works as specified – just not as intended. Now the question is: What exactly do you want the regex to do?

    For example, if what you really want is:

    Match b alone, unless it’s preceded by a and some non-word characters, in which case match everything from a to b, then use

    b|a\W*b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following regex expression to match html links: <a\s*href=['|](http:\/\/(.*?)\S['|]> it kind of
To outline: I have a parser that grabs Cell references using the following regex
In Python compiled regex patterns have a findall method that does the following: Return
I have the following Regex: Regex regex = new Regex(@(?<g1>a?)(?<g2>a?)(?<g3>b?)(?<g4>b?)); and a string string
I have been doing some searching for a regex that can be used as
I have the following RegEx to replace the entire querystring for a string with
i am working on a regex match function in python. i have the following
I have a string like this This:string:must~:be:split:when:previous:char:is:not~:this I need to split the line with
How to do the following using Regex String Str=11111111; if(Str.Contains(0)) MessageBox.Show(Not matching); else MessageBox.Show(Matching);
I have the following string, which matches the following RegEx string. I would like

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.