+ 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

15th Sep 2020, 9:28 PM
Rodrigo Tallar
Rodrigo Tallar - avatar
15 Antworten
+ 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!
15th Sep 2020, 10:53 PM
Namit Jain
Namit Jain - avatar
+ 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
15th Sep 2020, 10:27 PM
Abhay
Abhay - avatar
+ 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 😃
15th Sep 2020, 10:39 PM
Namit Jain
Namit Jain - avatar
+ 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
15th Sep 2020, 11:01 PM
Abhay
Abhay - avatar
+ 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 🙂
15th Sep 2020, 9:43 PM
Namit Jain
Namit Jain - avatar
+ 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
15th Sep 2020, 10:01 PM
Rodrigo Tallar
Rodrigo Tallar - avatar
+ 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)
15th Sep 2020, 10:44 PM
Namit Jain
Namit Jain - avatar
+ 1
Abhay I am talking about the second "-" The first one is perfectly alright!
15th Sep 2020, 10:46 PM
Namit Jain
Namit Jain - avatar
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?
15th Sep 2020, 10:42 PM
Rodrigo Tallar
Rodrigo Tallar - avatar
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!-_]+"
15th Sep 2020, 10:43 PM
Abhay
Abhay - avatar
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
15th Sep 2020, 10:44 PM
Abhay
Abhay - avatar
0
Namit Jain how so? a-z accepts a b c d e f not "." (dots)
15th Sep 2020, 10:45 PM
Abhay
Abhay - avatar
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
15th Sep 2020, 10:50 PM
Abhay
Abhay - avatar
0
Updated the code and escaped '-' Now it doesn't match dots, but it still matches the string before them. Any ideas?
15th Sep 2020, 10:50 PM
Rodrigo Tallar
Rodrigo Tallar - avatar