Crazy Horse
asked on
regular expression for 10 digit number
I am trying to simplify my telephone number validation and I just want a regular expression to check that the input is only numbers and must be 10 digits. I am not very clued up on regular expressions/preg_match and any assistance would be appreciated.
Also, if anyone can point me in the direction of a good (not difficult to understand) tutorial on basic regular expressions I would be grateful.
Also, if anyone can point me in the direction of a good (not difficult to understand) tutorial on basic regular expressions I would be grateful.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And a good playpen https://www.regex101.com/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A lot of really helpful answers here, thank you!
Two of the most important answers were omitted.
https://xkcd.com/1171/
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-top
Basically it comes down to this. There is no formal way to test regular expressions. Instead, you have to "roll your own" tests like those shown in this article.
TL;DR Regular Expressions (indeed, almost any kind of string processing algorithms) are very, very hard to get right. If they can be avoided, it's probably wise to design an app that can avoid them! If they can't be avoided, normalize and thereby minimize the string processing.
https://xkcd.com/1171/
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-top
Basically it comes down to this. There is no formal way to test regular expressions. Instead, you have to "roll your own" tests like those shown in this article.
TL;DR Regular Expressions (indeed, almost any kind of string processing algorithms) are very, very hard to get right. If they can be avoided, it's probably wise to design an app that can avoid them! If they can't be avoided, normalize and thereby minimize the string processing.
http://www.regular-expressions.info/tutorial.html
http://www.regular-expressions.info/quickstart.html
This seem OK, but I haven't used it:
https://regexone.com/
A one-page reference (useful after you've mastered the basics and the jargon) is here:
http://www.cbs.dtu.dk/courses/27610/regular-expressions-cheat-sheet-v2.pdf