What is the difference between [0-9]{1,2} and ^[0-9]{1,2}$?
If we remove ^ and $, what will the effect be in this case?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Those characters are called anchors and they bind to specific positions in the string. If you’re not discussing multi-line regexes, they will bind to the beginning (for
^) and end (for$) of the string.That means that
^[0-9]{1,2}$is a string that consists of exactly one or two digits and nothing else, while[0-9]{1,2}is a string of any size containing one or two consecutive digits in it anywhere.So the first will match
1,77,96,42and so on. The latter will also match any of those with any amount of text on either side, likepaxdiablo is number 1,calling car 54, where are youand so on.If your regex engine is capable of handling multi-line input, the meaning is usually slightly changed.
^will bind to either the start of the string or a zero-width point following an end of line character. Similarly,$will bind to the end of string and any zero-width point before an end on line.In those cases, if you want to only match the start or end of the string, you’ll probably find you can use
\Aand\Z.