Skip to main content

Delimited patterns

Delimited pattern#

Method Pattern.delimited() returns regular expression as string, representing the pattern in format compatible with with PHP PCRE methods.

echo pattern('https://github.com#heading')->delimited();
%https://github.com#heading%

PCRE-style patterns#

If the Pattern was constructed with PcrePattern::of(), method delimited() returns the pattern unchanged.

PcrePattern::of('#Welcome/Or not#')->delimited();
#Welcome/Or not#

However, certain regular expressions will be modified to allow safe execution. For example, pattern /\c\/
fails matching in vanilla PHP, so such pattern is delimited in other ways, to ensure proper execution.

preg_match('/\c\/', $subject); // fails matching
PcrePattern::of('/\c\/')->test($subject); // works just fine

Modifiers#

There are two ways of using patterns with PCRE modifiers:

  • Either pass a second argument to pattern()/Pattern::of():

    // global function
    pattern('[A-Z][a-z]+', 'i')->test($subject);
    // static method
    Pattern::of('[A-Z][a-z]+', 'i')->test($subject);
    // prepared patterns
    Pattern::inject('[A-Z]@', [$_GET['name']], 'i')->test($subject);
  • or use an old-school pattern:

    PcrePattern::of('/[A-Z][a-z]+/i')->search($subject)->first();
Last updated on