简体   繁体   中英

preg_match string

Can someone explain me the meaning of this pattern.

preg_match(/'^(d{1,2}([az]+))(?:s*)S (?=200[0-9])/','21st March 2006','$matches);

So correct me if I'm wrong:

    ^        = beginning of the line   
    d{1,2}   = digit with minimum 1 and maximum 2 digits  
    ([a-z]+) = one or more letters from a-z
    (?:s*)S  = no idea...  
    (?=      = no idea...
    200[0-9] = a number, starting with 200 and ending with a number (0-9)

Can someone complete this list?

regular-exressions.info is very helpful resource.

/'^(d{1,2}([az]+))(?:s*)S (?=200[0-9])/

(?:regex) are non-capturing parentheses; They aren't very useful in your example, but could be used to expres things like (?:bar)+ , to mean 1 or more bar s

(?=regex) does a positive lookahead, but matches the position not the contents. So (?=200[0-9]) in your example makes the regex match only dates in the previous decade, without matching the year itself.

Here's a nice diagram courtesy of strfriend :

^(d {1,2}([a-z] +))(?:s *)S(?= 200 [0-9])

But I think you probably meant ^(\\d{1,2}([az]+))(?:\\s*)\\S (?=200[0-9]) with the backslashes, which gives this diagram :

^(\\ d {1,2}([a-z] +))(?:\\ s *)\\ S(?= 200 [0-9])

That is, this regexp matches the beginning of the string, followed by one or two digits, one or more lowercase letters, zero or more whitespace characters, one non-whitespace character and a space. Also, all this has to be followed by a number between 2000 and 2009, although that number is not actually matched by the regexp — it's only a look-ahead assertion. Also, the leading digits and letters are captures into $matches[1] , and just the letters into $matches[2] .

For more information on PHP's PCRE regexp syntax, see http://php.net/manual/en/pcre.pattern.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM