Automatic delimiters

This chapter doesn't contain PHP code snippets, because PHP PCRE require delimiters. There is no way to omit them in Vanilla PHP.

Thanks to automatic delimiters, one can use regular expressions without brain strain and without coming up with a suitable delimiter.


Adding delimiters

To change undelimited pattern into a delimited one - use delimiter() method;

pattern('Welcome/Or not')->delimiter();
#Welcome/Or not#

delimiter() returns the pattern unchanged, as if it was constructed with Pattern::pcre(), instead of Pattern::of().


There are two ways of passing flags:

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

// global function
pattern('[A-Z][a-z]+', 'i')->match($subject)->first();
// static method
Pattern::of('[A-Z][a-z]+', 'i')->match($subject)->first();

or use old-school pattern:


I want to break it

T-Regx has a set of predefined, suitable delimiters (like /, #, ~, %, etc.) and simply uses the first one, that doesn't occur in your pattern. If you exhaust each of them; if you use every possible, predefined, suitable delimiter - it will throw ExplicitDelimiterRequiredException.

In that case, you simply have to use an explicit delimiter with Pattern::pcre().

If you think another automatic delimiter can be used, please create a github issue.

Last updated on