Search code examples
phpregexvalidationdelimiterpassword-strength

What does it mean when a regular expression is delimited by @ symbols?


I found this script for password validation, but I don't know, why is "@" inside patterns. Can you explain me it? Thanks

$uppercase = preg_match('@[A-Z]@', $_POST['password']);
$lowercase = preg_match('@[a-z]@', $_POST['password']);
$number = preg_match('@[0-9]@', $_POST['password']);

if (!$uppercase || !$lowercase || !$number || strlen($_POST['password']) < 8)
{
    $message .= "Password must contains lowercase letter, uppercase letter, digit and minimal length is 8 characters. <br/>";
}

Solution

  • In many regex libraries, the "delimiter" is actually completely up to the programmer, and totally arbitrary. Basically, whatever the first char in the pattern is, that's the delimiter and when it shows up again (unescaped) the pattern is considered complete (optionally with some flags following the closing delimiter).

    You can consider it a matter of programmer style/taste, or maybe they got in the habit on a project where the patterns needed / in the pattern and they didn't want to escape them all the time.