Difference between r'^specific expression$' and r'specific expression' [duplicate]


Difference between r'^specific expression$' and r'specific expression' [duplicate]



This question already has an answer here:



My doubt is that I came across a regex which checks whether a password is strong or not. What is the impact of ^ and $ in this expression.


a = compile(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$')



It has ^ and $ signs in it. But the below code works the same as above.


a = compile(r'(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}')



If so why are they been used in the above code. Or is there reason for its usage. Thanks in advance!



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You would not ask the difference if you knew why caret and dollar sign are being used. Please check the marked question.
– revo
Jul 1 at 10:27




2 Answers
2



The ^ means “beginning of a line” and the $ means “end of a line”.
In your case, every match is a line so you don't have any problem.


^


$





Is that good to use both variants for the password checking program right?
– Mothish
Jul 1 at 10:13






Using these symbols is more secure because you are sure to match a single and complete line.
– Blincer
Jul 1 at 10:15





By using ^ and $ we can match string with no new line characters in them ?
– Mothish
Jul 1 at 10:24






No you cannot, the first occurence of new line is matched by $.
– Blincer
Jul 1 at 10:27


$



^ is followed by the string or pattern by which the string will be started with and $ follows the string or pattern by which the string will be ended with. For your case your regex matched with the pattern of the string without regarding the starting or ending portion.


^


$


regex





^ and $ ensures that there are no new line characters right?
– Mothish
Jul 1 at 10:24





That depends. Please read about re.DOTALL and re.MULTILINE.
– Matthias
Jul 1 at 10:33



re.DOTALL


re.MULTILINE

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API