What do the characters in preg_match mean?

I have this expression in a piece of code that I borrowed offline. This forces new users to have a password that requires not only numbers with top + bottom +, but they must be in that order! If I enter the bottom + top + number, it fails!

if (preg_match("/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $pw_clean, $matches)) {

I searched on the Internet but cannot find a resource that tells me what some characters mean. I see that the template is preg_match ("/ some expression /", yourstring, your match).

What does it mean:

1.  ^          -  ???
2.  .*         -  ???
3.  (?=.{4,})  -  requires 4 characters minimum
4.  (?.*[0-9]) -  requires it to have numbers
5.  (?=.*[a-z])-  requires it to have lowercase
6.  (?=.*[A-Z])-  requires it to have uppercase
7.  .*$        -  ???
+5
source share
3 answers

. , . regular-expressions.info. , - . / , .


1: ^ , " //".

  • [], : . (, [^ab] -, ab)

2: . * :

  • , \n.
  • : " ".

.*, " , , ".

7: $ , , : " ".


Edit:

( ) - . (?=), , , . , , , . ?
: foo(?=bar) foo, bar. bar , foo.

, :

/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/

Reads as:
        ^.* From Start, capture 0-many of any character
  (?=.{4,}) if there are at least 4 of anything following this
(?=.*[0-9]) if there is: 0-many of any, ending with an integer following
(?=.*[a-z]) if there is: 0-many of any, ending with a lowercase letter following
(?=.*[A-Z]) if there is: 0-many of any, ending with an uppercase letter following
        .*$ 0-many of anything preceding the End

, - . . script . , . , , .

<pre>
<?php
// Only the last 3 fail, as they should. You claim the first does not work?
$subjects = array("aaB1", "Baa1", "1Baa", "1aaB", "aa1B", "aa11", "aaBB", "aB1");

foreach($subjects as $s)
{
    $res = preg_match("/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $s, $matches);
    echo "result: ";
    print_r($res);

    echo "<br>";
    print_r($matches);
    echo "<hr>";
}

- : https://regex101.com/

+13

To use regular expressions, you first need to learn the syntax. This syntax consists of a series of letters, numbers, periods, hyphens, and special characters that we can group using different parentheses.

Have a look at this link Getting Started with PHP Regular Expressions . An easy way to learn regular expressions.

+1
source

All Articles