0

I have RegExp condition is /^([0-9]*\.?[0-9])*$/ to test string. My string are first is 1.2.840.346991791506342.1482500253171661(large string) & second is 1.2.3.201922311129.10038 (short string).

It successfully search as both strings are OK.

But when I add space at the last of second string short string it's showing invalid that is right conclusion.

But when I add space in first string it should display invalid string as per code but it gets hanged why it is showing hang?

RegExp limit is exhausted? What will be the solution?

You can check this in notepad+ for testing purpose ^([0-9]*\.?[0-9])*$ use this formula directly.

1
  • Did you just find a bug in some regexp implementations ? :D Because this looks like pretty much a bug to me Commented May 2, 2019 at 11:50

1 Answer 1

3

The way you have written your regex, having nested quantifier is leading it to catastrophic backtracking leading it to hang/timeout.

Catastrophic Backtracking Demo

You need to simplify your regex to something like this,

^[0-9]*(?:\.[0-9]+)*$

Let me know if this regex preserves your pattern.

Regex Demo not running into timeout

You should in general avoid over nesting quantifiers in your regex, and rather try writing them in a simpler manner as much as you can. Even for short string like 1.2.840.3469931313.313, see how much steps your regex is taking,

135228 steps taken

and if you increase your string length little bit, then it runs into timeout/catastrophic backtracking.

Sign up to request clarification or add additional context in comments.

9 Comments

Glad to know its working well for you now. And ?: is used to turn a group into non-grouping pattern and it's a good idea to do that unless you really need that group and it does not change the logical meaning of regex. Also consider accepting any answer if it helped :)
Kumar ,if have same string as above but I want to remove space at front & back end of string ,what will be regex?
@VikramNarke: Your question is a bit not clear. The regex I have given in my answer will not allow any space already. But if you want to allow space(s) at the beginning of text and/or at the end of text, you can use this regex ^\s*[0-9]*(?:\.[0-9]+)*\s*$
If u add sapce in a string (start or end)1.2.345.3456.1223.1space space space .it should remove the space & result should be displayed as 1.2.345.3456.1223.1 without any space . Whatever space at start & end it should be removed
Pushpesh:Thanks for your great help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.