3

What is pattern mean in PHP:

'#^/abc/(?P<abc>[^/]++)$#si'; // please expand this pattern's meaning.

and what are strings can match of this pattern with preg_match_all?

brandonscript
  • 57,554
  • 29
  • 142
  • 204
Dos
  • 771
  • 9
  • 23

1 Answers1

5

This pattern '#^/abc/(?P<abc>[^/]++)$#si'; broken down like so:

/^\/abc\/(?P<abc>[^\/]++)$/si

^ assert position at start of the string
\/ matches the character / literally
abc matches the characters abc literally (case insensitive)
\/ matches the character / literally
(?P<abc>[^\/]++) Named capturing group abc
    [^\/]++ match a single character not present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, without giving back [possessive]
        \/ matches the character / literally
$ assert position at end of the string
s modifier: single line. Dot matches newline characters
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

(Source: http://regex101.com/r/hS6qE3 -- make sure you escape your / on the site as it assumes / is the php delimiter. In your pattern example, # is the delimiter instead, which is then wrapped in quotes and the string terminator ;. The actual expression is simply ^/abc/(?P<abc>[^/]++)$ with the single-line and case insensitive modifiers.)

Note the use of two ++ signs as well, which changes the behavior from greedy to possessive.

An example of a string that will match:

/abc/somethingelsenotafrontslash

You can read a quick explanation about preg_match_all here

Community
  • 1
  • 1
brandonscript
  • 57,554
  • 29
  • 142
  • 204