Regex not working as expected ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regex not working as expected !

Hi all, I wanted to create a regex for the pattern :- * 5 digits underscore 11 digits e.g. 12345_12345678901 My attempt :- /\d{5}[ _ ]\d{11}/ Failure case :- It is giving me true even if the length is increasing. E.g. 123456_12345678901,12345_1234567890123

29th Apr 2022, 8:50 AM
Ashutosh Raturi
Ashutosh Raturi - avatar
6 Answers
+ 3
Ashutosh Raturi put \b at the start and at the end of your expression
29th Apr 2022, 9:05 AM
Bob_Li
Bob_Li - avatar
+ 2
Ashutosh Raturi, as FF9900 said: /^\d{5}[_]\d{11}$/ ^ for start $ for end You should also remove spaces from square brackets
29th Apr 2022, 9:22 AM
OrHy3
OrHy3 - avatar
+ 1
FF9900 😎 online regex testers are super useful.
29th Apr 2022, 9:10 AM
Bob_Li
Bob_Li - avatar
0
FF9900 can you please tell me how to do that?
29th Apr 2022, 8:56 AM
Ashutosh Raturi
Ashutosh Raturi - avatar
0
Thanks to everyone for quick help 😃
29th Apr 2022, 9:48 AM
Ashutosh Raturi
Ashutosh Raturi - avatar
0
Ashutosh Raturi OrHy3 's /^\d{5}_\d{11}$/ is more strict. it will not match #12345_12345678901 or -12345_12345678901- while /\b\d{5}_\d{11}\b/ will match them as FF9900 said, it depends on your use case.
29th Apr 2022, 9:53 AM
Bob_Li
Bob_Li - avatar