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 that isn't working. I want to match the string
Simple regex question. I have a string on the following format: this is a
I need some help writing a regex. I have the following strings, xxx.yyy.wwwwwaaa_IN_123 xxx.rrrttttt_IN_12355
I have the following regex date validator but it is allowing the following date:
I have the following regex which validates dates: //are not both dates if (!methods._isDate(first[0].value)
I have written the following regex to match a set of e-mails from HTML
I have the following string and regex: var string = Dear [to name], [your
I have the following regex: ({(?!d})(?!{)(?<!{{)[^}]+}) it macthes any digit/string inside curly braces {},
I have the following regex: (?<=\.\d+?)0+(?=\D|$) I'm running it against a string which contains
I have the following regex type in my xsd file: <xsd:simpleType name="Host"> <xsd:restriction base="xsd:string">

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.