How to validate phone numbers using regex

I’m trying to put together a comprehensive regex to validate phone numbers. Ideally it would handle international formats, but it must handle US formats, including the following: 1-234-567-8901 1-234-567-8901 x1234 1-234-567-8901 ext1234 1 (234) 567-8901 1.234.567.8901 1/234/567/8901 12345678901 I’ll answer with my current attempt, but I’m hoping somebody has something better and/or more elegant. 42 … Read more

What is a non-capturing group in regular expressions?

How are non-capturing groups, i.e., (?:), used in regular expressions and what are they good for? 1 18 Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it… (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? … I would get the following result: Match “http://stackoverflow.com/” Group 1: “http” … Read more

How can I validate an email address using a regular expression?

Over the years I have slowly developed a regular expression that validates most email addresses correctly, assuming they don’t use an IP address as the server part. I use it in several PHP programs, and it works most of the time. However, from time to time I get contacted by someone that is having trouble … Read more

Regular expression to match a line that doesn’t contain a word

I know it’s possible to match a word and then reverse the matches using other tools (e.g. grep -v). However, is it possible to match lines that do not contain a specific word, e.g. hede, using a regular expression? Input: hoho hihi haha hede Code: grep “<Regex for ‘doesn’t contain hede’>” input Desired output: hoho … Read more