Reading Regular Expressions for Beginners

I read basic regular expressions on different sites to learn them. my problem is that I do not understand some of them. here is an example I'm looking at to check the email address from w3schools

$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
   $emailErr = "Invalid email format"; 
}

I don’t understand the part, [\w\-]+as I understand it, it says "a string that has at least alphanumeric". can you give me a clear explanation for this?

+3
source share
3 answers

The class [\w\-](or, more precisely, without unnecessary shielding, [\w-]) means

  • \w- a word symbol ; any letter, number or underscore or ...
  • - any hyphen

[\w-]+ " , , ".

, W3Schools. http://www.regular-expressions.info/ - (IMHO).

+1

:

  • \w - , , . [A-Za-z0-9_]
  • \w\- \w ( , )
  • [\w\-]+ . , 9@email.com , @email.com .

Also, depending on your use case, you might be interested in this discussion on SO about why relying on regular expressions to check email addresses might be a bad idea:

Using regex to validate email addresses

+1
source

All Articles