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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:46:38+00:00 2026-05-12T09:46:38+00:00

A little regex help please. Why are these different? Regex.Replace((999) 555-0000 /x ext123, /x.*|[^0-9],

  • 0

A little regex help please.

Why are these different?

Regex.Replace("(999) 555-0000 /x ext123", "/x.*|[^0-9]", String.Empty)
"9995550000"


Regex.Replace("(999) 555-0000 /x ext123", "[^0-9]|/x.*", String.Empty)
"9995550000123"

I thought the pipe operator did not care about order… or maybe there is something else that can explain this?

  • 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-12T09:46:38+00:00Added an answer on May 12, 2026 at 9:46 am

    I think you’ve got the wrong idea about alternation (i.e., the pipe). In a pure DFA regex implementation, it’s true that alternation favors the longest match no matter how the alternatives are ordered. In other words, the whole regex, whether it contains alternation or not, always returns the earliest and longest possible match–the “leftmost-longest” rule.

    However, the regex implementations in most of today’s popular programming languages, including .NET, are what Friedl calls Traditional NFA engines. One of the most important differences between them and DFA engines is that alternation is not greedy; it attempts the alternatives in the order they’re listed and stops as soon as one of them matches. The only thing that will cause it to change its mind is if the match fails at a later point in the regex, forcing it to backtrack into the alternation.

    Note that if you change the [^0-9] to [^0-9]+ in both regexes you’ll get the same result from both–but not the one you want. (I’m assuming the /x.* alternative is supposed to match–and remove–the rest of the string, including the extension number.) I’d suggest something like this:

    "[^0-9/]+|/x.*$"
    

    That way, neither alternative can even start to match what the other one matches. Not only will that will prevent the kind of confusion you’re experiencing, it avoids potential performance bottlenecks. One of the other major differences between DFA’s and NFA’s is that badly-written NFA’s are prone to serious (even catastrophic) performance problems, and sloppy alternations are one of the easiest ways to trigger them.

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

Sidebar

Related Questions

Need a little help with a regex I'm trying to match a string that
I am not a regex guy, so I need a little help, just cant
need a little help with extracting part of the URL via regex I have
I came across this excellent little RegEx to replace URLs (not hyper-links) in plain
I need a little help with some regex I have. Basically I have a
i have this little class to make a multiple replace on a string: import
I need a little help on a regex pattern. I have some strings ending
I have a little problem with regex. I want to clean up a string.
Stuggling a little bit with the RegEx, I've got 4 codes in a string
I have 0 experience with python, very little with regex and I'm trying to

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.