+ 1

[Solved] Why this regular expression doesn't work on specific cases

From prismJS I got a regex that matches all types of numbers. I modified it to match the first occurrence of the number. But it doesnt work for the number with underscores (like 2_4_4, which is valid) RegExp with test cases: https://code.sololearn.com/Wa5A5A5a09a4 What is the problem with this regex? (this regex is too long to read. There is a visual representation of this here: https://tinyurl.com/8efuv5jf )

11th Jul 2021, 6:13 AM
Roopesh
Roopesh - avatar
17 Answers
+ 2
after digit_digit the loop to _ is needed but missing.
11th Jul 2021, 11:24 AM
Oma Falk
Oma Falk - avatar
11th Jul 2021, 7:11 AM
Oma Falk
Oma Falk - avatar
+ 2
it allows one underscore. 2_4 is valid.
11th Jul 2021, 6:42 AM
Oma Falk
Oma Falk - avatar
+ 2
on the bottom of your graphics I see u need 2_4 and then a dot 2_4.2_4 or again n_n 2_46_8 why u need a dot after 2_4? replace by - could help.
11th Jul 2021, 6:59 AM
Oma Falk
Oma Falk - avatar
+ 2
That was fun. Congrats
11th Jul 2021, 12:34 PM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk 34_5_.3 is not a valid number in JS. There should be atleast one digit after underscore.
11th Jul 2021, 12:14 PM
Roopesh
Roopesh - avatar
0
Oma Falk that's the problem. It should match all of them. The output should be 2_3_4 not 2_3
11th Jul 2021, 6:48 AM
Roopesh
Roopesh - avatar
0
Oma Falk I think you misunderstood. I'm creating a parser. And I will have input like `2_5_6 + 7` and I need a regex that returns the first number of the input (that is 2_5_6). So that would be like this: ``` var input = "2_5_6 + 7" var firstNumber = input.match(numberFinderRegExp)[0]; console.log(firstNumber)// should return '2_5_6' ```
11th Jul 2021, 9:25 AM
Roopesh
Roopesh - avatar
0
It seems that your solution solves it. But it doesn't work on decimal number sequences. If the test case was "335_772_37.62_727 + 6", it only returns "335_772_37" whereas it should return "335_772_37.62_727"
11th Jul 2021, 9:39 AM
Roopesh
Roopesh - avatar
0
Oma Falk I've updated the test code check it again
11th Jul 2021, 10:14 AM
Roopesh
Roopesh - avatar
0
but now 1_2.3_4.5_6 is valid ohhh it Red. not valid.
11th Jul 2021, 10:39 AM
Oma Falk
Oma Falk - avatar
0
Oma Falk all the given test cases are valid JS numbers. The green colour say that the regex matched every character of the test. While red indicate that regex failed to match all characters in the test
11th Jul 2021, 10:55 AM
Roopesh
Roopesh - avatar
0
It doesn't check if the number is valid but it search for the valid ones. In the code all give test cases are valid
11th Jul 2021, 10:56 AM
Roopesh
Roopesh - avatar
11th Jul 2021, 11:32 AM
Oma Falk
Oma Falk - avatar
0
Oma Falk I actually got a solution. As you said after digit_digit there should be a loop to _ again. I added it. Thanks for clue
11th Jul 2021, 12:15 PM
Roopesh
Roopesh - avatar
0
Solution: https://tinyurl.com/d7zh4ah5 . I don't know how concrete the solution is, but will be ready to encounter flaws in future
11th Jul 2021, 12:18 PM
Roopesh
Roopesh - avatar
0
Oma Falk welcome
11th Jul 2021, 12:37 PM
Roopesh
Roopesh - avatar