Skip to main content

Validate a pattern

note

This chapter doesn't contain PHP code snippets, because PHP PCRE doesn't provide a way to validate patterns.

You can check whether a pattern is valid with pattern()->valid() method.

pattern('I am a valid pattern')->valid();
true
note

pattern()->valid() doesn't throw any exception nor issues any warnings. It only returns true/false.

pattern('I am an (( invalid }} pattern')->valid();
false

Old-school patterns#

Undelimited patterns can be validated with pattern()/Pattern::of(). To validate old-school patterns (e.g. /Hello/i), use Pattern::pcre():

Pattern::pcre('/I am an valid pattern/m')->valid();
true
Last updated on