What's T-Regx?
Documentation for version: 0.41.2
T-Regx is a lightweight, high-level library for regular expressions in PHP. It's designed to be suitable for simple projects and enterprise solutions.
The name "T-Regx" is a combination of words "Regex" and "T-Rex" (Tyrannosaurus Rex).
Main features of T-Regx are:
- Single-point entry
Pattern
class - Regular expressions without delimiters (so
Pattern::of("^foo")
instead ofpreg_match("/^foo/")
) - UTF-8 support out of the box
- Prepared Patterns handling unsafe characters (e.g. user input, delicate values)
- Both replacing and matching is with detailed
Detail
object - Uses PHP regular expressions under the hood, but doesn't leak any of its interface or flaws
Surpasses PHP in many ways:
- Doesn't use PHP warning/error/notice/fatal system, uses only exceptions, for example
MalformedPatternException
- Doesn't use any flags, default arguments or varargs, everything is done with methods
- Doesn't return
false
ornull
on error, but throws a suitable exception instead (for exampleNonexistentGroupException
) - Doesn't return
null
or""
to indicate an unmatched group, but usesGroup.matched()
and other methods instead
#
Example usages of T-Regx#
Example usage of matchingWhen calling multiple methods on the same $matcher
object, T-Regx doesn't make any unnecessary calls to underlying
PHP implementations.
#
Example usage of replacing#
Example usage of splitting#
Example of prepared patternsThis example illustrates a pattern that allows us to match string enclosed in double quote "
or a backtick `
, for example "foo"
or `foo`
, but not "foo`
and not `foo"
.
Additionally, we want the word to be anything we want, including data that potentially holds special regular expression values.
Method Pattern.test()
returns true
if, and only if a pattern matches the given subject. In this example,
we can see that despite the dot character used in "my.word"
, the subject 'content with "my!word"'
isn't matched.
That's because prepared patterns (such as patterns created with Pattern::inject()
) treat each value literally,
not as regular expression special characters.
You can read more about prepared patterns in Handling user input.
#
Code completion with IDEBecause Pattern
is designed with methods and objects, IDE suggestions can be very helpful when developing applications
with Pattern
. Proper suggestions from IDE reduce time spent of reading documentations and finding the correct
syntax or notation.
#
Error handling in T-Regx#
Examples of a missing group#
Examples of error handling in T-RegxT-Regx doesn't interfere with userspace in any way. After using Pattern
or Matcher
with pattern
or subject which can't be matched because of malformed unicode encoding, or perhaps because of
catastrophic backtracking, a suitable exception will be thrown, but the userspace will be left intact,
so calling preg_last_error()
won't return errors.
T-Regx doesn't override error handlers or exception handlers, since that could greatly pollute userspace and render the client application less reliable.