+ 3
[Regex] Matching doubts
Can someone explain why the first example match and how could I match any number from 1 to 31, in the code you can see what I tried, dont know why it only matches one digit though. https://code.sololearn.com/cymM0q4aMw5C/?ref=app
15 Answers
+ 3
Abhay 😆
A misunderstanding. 😲
I am saying that in the FIRST expression there are 2 "-" if you see carefully, the first "-" between a and z is fine but the second "-" was creating problems! So, using a "\" will work perfectly!
Rodrigo Tallar That is because the regex found "_asd" in its match! It broke as soon as a "." came in the way!
+ 2
0?[1-9]|[1-2]\d|30|31
In the second match for string 31
It is checking if it starts with 0 or not and then finds 3 from the expression [1-9] so you get your first and only match and it doesn't needs to check other "or" conditions
+ 2
Rodrigo Tallar
In the first case:
I have given the reason for the match!
But in case if the string starts from a dog '.' then just remove that '-' sign from the variable local, and see the result! The '-' is also used in 'a-z' or 'A-Z' and all. So the system got confused!
And for the second match Abhay has given the answer 😃
+ 2
Namit Jain thks a lot! didn't noticed that if a-z has a special meaning and stands for all characters between a and z ,so was with +-/ which was checking for characters between + and / and escaping it diminished that special meaning
+ 1
Just add a '+' sign in line 4
And you will get the expected results!
The first regex will check only if the first letter is there in the 'local' variable or not 🙂
+ 1
Adding '+' to the fourth line makes numbers like 292 match, which clearly are not between 1-31, already fixed it placing a '#x27; at the end. Not sure what you mean with the second regex, but starting the string with a '.' still makes it match
+ 1
No Abhay
The reason for matching of strings beginning with dots in the first case is due to the presence of "-". (read my answer)
+ 1
Abhay I am talking about the second "-"
The first one is perfectly alright!
0
Already fix that part with '#x27; at the end.
0? Is because I want numbers like 01-09 to be accepted as well as 1-9
For the second regex I should have escaped - right?
0
In the first one
Even if you use this it will match whole expression and the main character that I haven't been able to understand yet from the following is "!" ,if you remove it won't match dots ,but keeping it matches dots
r"[a-z!-_]+"
0
yea that's what I said it checks starting with 0 or not ,even if 0 doesn't exist match will continue checking for other part of expression
0
Namit Jain how so? a-z accepts a b c d e f not "." (dots)
0
Namit Jain lol ! I am talking about first one and that's the only concern for me , first one matches whole string when it shouldn't for dots ,but it does possibly because of exclamation mark
Second one I already explained ,
Maybe someone else can explain better than me and possibly answer my question as well
0
Updated the code and escaped '-' Now it doesn't match dots, but it still matches the string before them. Any ideas?