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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:01:01+00:00 2026-06-13T22:01:01+00:00

Never thought it would be possible to write a Regex which never returns. The

  • 0

Never thought it would be possible to write a Regex which never returns.

The Regex

/^((?:\d|\w{1,2}[-\d\s])(?:[-\s\d]|\w{1,2}[-\d\s])*\d)$/

was built to match numbers which start with a digit or two letters followed by a dash a blank or digits and end with a number. In between the starting pattern could be repeated or a whitespace or dash could occur.

Examples: 1234 , de-12943, EN – 12de -50

Following example code will not terminate:

ruby

#!/usr/bin/ruby
string = "101000000750000000000000000000000001000038127OXMOO0OOOOO00000000000N9"
re = /^((?:\d|\w{1,2}[-\d\s])(?:[-\s\d]|\w{1,2}[-\d\s])*\d)$/
p re.match("101000000750000000000000000000000001000038127OXMOO0OOOOO00000000000N9")

scala

"""^((?:\d|\w{1,2}[-\d\s])(?:[-\s\d]|\w{1,2}[-\d\s])*\d)$""".r findFirstIn "101000000750000000000000000000000001000038127OXMOO0OOOOO00000000000N9"

Removing the Anchors (^, $) lets the regex terminate quickly.

Tried with Ruby and Scala.

What’s happening there?
Should’t the anchors lead to faster termination?

  • 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-13T22:01:02+00:00Added an answer on June 13, 2026 at 10:01 pm

    Firstly, \w is not a letter, but [a-zA-Z0-9_]. So if you really just want letters there make that [a-zA-Z].

    Secondly, I suppose you might have a case of catastrophic backtracking.

    Your regex obviously does not go past OXM, because there is no way to match three consecutive letters in your pattern. And if you remove the $ anchor, the regex will gladly match there, but when you leave it then the regex will fail and start to backtrack.

    So suppose it matched the OX with \w{1,2} and failed. Then it will throw away the last repetition of the whole second non-capturing group and go back one step, where it matched 7 with with [-\s\d]. Now it will try to match 7O or 7 with \w{1,2} instead, but then again fails to match [-\d\s] against X or O, respectively. Another step back, it tries to rematch 27 or 2 with \w{1,2} and fails again. And so on and so on. The further you go back, it might be possible again to match the [-\d\s] against a letter, and then the engine will go all the way forward to OXM again and start the fun again. When backtracking finally reaches the beginning of the string and your very first alternation, it will try all three options for that alternation, too, and will do the whole thing over and over again.

    Let me try to visualize the first steps of the backtracking by writing out which alternations are used in the repetition. The first of each two lines is the tested string, the second contains the corresponding regex constructs used. Each attempt fails at the last character.

    ... 1       2       7       O
    ... [-\s\d] [-\s\d] [-\s\d] [-\s\d] 
    
    ... 1       2       7       OX    M
    ... [-\s\d] [-\s\d] [-\s\d] \w{2} [-\d\s]
    
    ... 1       2       7       O     X
    ... [-\s\d] [-\s\d] [-\s\d] \w{1} [-\d\s]
    
    ... 1       2       7O    X
    ... [-\s\d] [-\s\d] \w{2} [-\d\s]
    
    ... 1       2       7     O
    ... [-\s\d] [-\s\d] \w{1} [-\d\s]
    
    ... 1       27    O      
    ... [-\s\d] \w{2} [-\d\s]
    
    ... 1       2     7       O
    ... [-\s\d] \w{1} [-\d\s] [-\s\d]
    
    ... 1       2     7       OX    M
    ... [-\s\d] \w{1} [-\s\d] \w{2} [-\d\s]
    
    ... 1       2     7O    X
    ... [-\s\d] \w{1} \w{2} [-\d\s]
    

    And so on. I hope you get the idea. It’s hard to visualize it in a few lines of ASCII.

    I suppose, just changing \w to the appropriate character group might already solve the problem, because there are less equivalent combinations. Try it out.

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

Sidebar

Related Questions

I never thought it would happen to me, but I ran into my first
Never thought I'd have this problem :) The following snippet of code works in
I have never thought about until recently, but I'm not sure why we call
When this quick one hour project came up I never thought that 2 days
I have zero experience with COM. I actually never thought, I'll need to do
I know the how to show th image on jsp file. But never thought
Recently We started on optimizing a database which never followed any standards. We though
Possible Duplicate: how to write hello world in assembler under windows? I've often heard
I've never really thought about this, but it helps with some security of something
I've recently discovered, to my astonishment (having never really thought about it before), machine-sorting

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.