Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading regular expression for beginner

I've read basic regular expressions on different websites to study them. my problem is that I don't understand some of them. here is an example I'm looking at to validate an e-mail 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\-]+ from my own understanding it says "string that has at least a alphanumeric". can you give me a clear explanation of this?

like image 602
zlloyd Avatar asked Nov 18 '25 08:11

zlloyd


1 Answers

The character class [\w\-] (or more accurately without the unnecessary escaping, [\w-]) means

  1. \w - Word character; any letter, number or underscore character, or...
  2. - any hyphen

Using [\w-]+ means "one or more letters, numbers, underscores or hyphens".

As mentioned in the comments above, don't use W3Schools. http://www.regular-expressions.info/ is the best resource available (IMHO).

like image 175
Phil Avatar answered Nov 20 '25 21:11

Phil