Quando si usano le funzioni PCRE, il pattern deve essere racchiuso da delimitatori. Un delimitatore può essere qualsiasi carattere non alfanumerico, esclusi il backslash e i caratteri di spazio (spazio, tab, ecc.).

Delimitatori usati spesso sono la barra "slash" (/), il cancelletto "hash" (#) e la tilde (~). I seguenti sono esempi di delimitazioni valide dei pattern:

/foo bar/

If the delimiter needs to be matched inside the pattern it must be escaped using a backslash. If the delimiter appears often inside the pattern, it is a good idea to choose another delimiter in order to increase readability.

The preg_quote() function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.

In addition to the aforementioned delimiters, it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively.

{this is a pattern}

You may add pattern modifiers after the ending delimiter. The following is an example of case-insensitive matching:


add a note add a note

User Contributed Notes 6 notes

Pedro Gimeno
5 years ago
Note that bracket style opening and closing delimiters aren't a 100% problem-free solution, as they need to be escaped when they aren't in matching pairs within the expression. That mismatch can happen when they appear inside character classes [...], as most meta-characters lose their special meaning. Consider these examples:

('{[{]}', ''); // Warning: preg_match(): No ending matching delimiter '}'
preg_match('{[}]}', ''); // Warning: preg_match(): Unknown modifier ']'
preg_match('{[}{]}', ''); // Warning: preg_match(): Unknown modifier ']'

Escaping them solves it:

('{[\{]}', ''); // OK
preg_match('{[}]}', ''); // OK
preg_match('{[\}\{]}', ''); // OK
5 years ago
preg_match('{[}]}', ''); // Warning: preg_match(): Unknown modifier ']'

preg_match('{[\}]}', ''); // OK
1 year ago
Note that angle brackets `<>` shouldn't be used as delimiters whenever you will have to invoke advanced clusters like atomic groups or lookbehinds because their including angle bracket doesn't come in pair and escaping doesn't help either.
rawr dot t dot regx at gmail dot com
1 year ago
You might also use T-Regx library, which has automatic delimiters:

4 years ago
The dirty little delimiter secret they don't tell you ->
Examples: Balanced delims {\d{2}Some\\{33\\}\w{5}} parses to
\d{2}Some\\{33\\}\w{5} and {\d{2}Some\{33\}\w{5}} parses to \d{2}Some{33}\w{5}. 
Un-Balanced delims +\d{2}Some\+33\+\w{5}+ parses to \d{2}Some+33+\w{5} and
+\d{2}Some\\+33\\+\w{5}+ won't parse because the delimiter is unescaped.
hmelin at ya dot ru
4 years ago
A delimiter can be any ASCII non-alphanumeric, non-backslash, non-whitespace character:  !"#$%&'*+,./:;=?@^_`|~-  and  ({[<>]})
To Top