I want to know what the difference is between these two regular expressions, what are the pro’s and con’s.
Example input (date) 31-12-2012.
Method A:
/(\d{2}-\d{2}-\d{4})/
And:
Method B:
^[0-9]{2}[-/][0-9]{2}[-/][0-9]{4}\$
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.
/, the second one doesn’t. For now, I assume that to be a copy/paste issue.^, A just agrees with"a datestring anyw00-00-0000where in the string".(), B does no such thing. As the entire match will be the0th item in a match, you could lose the unneeded()‘s.\dvs[0-9]-> see Avner’s answer.-as the day/month/year separator. Use that if you only expect-. If you expect BOTH-AND/, use[-/]as in B.$, A doesn’t. Use the one which applies. If I assume this is a copy/paste error ($being escaped because it is in a double quoted string for no good reason), it makes B match only a date because of the^regex$anchoring, and A a date string anywhere in the input. Once again, use the option that applies to your data.