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 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:
- Separates
Pattern
syntax and matching process - Unifies differences in PCRE between PHP versions
- Provides UTF-8 support out of the box
- Uses exceptions (instead of errors/warnings/fatals and flags)
- Regular expressions without delimiters (so
Pattern::of("^foo")
instead ofpreg_match("/^foo/")
) - Provides prepared patterns for handling unsafe characters (e.g. user input, delicate values)
- Has simplified, clean interface:
- exposes proper methods (instead of returning
null
,false
,""
or0
/-1
as magic values) - handles unnecessarily complex
preg_
state and output, and exposes it as:Pattern
andPatternList
for syntax and pattern buildingMatcher
,Detail
,Group
for matching (instead of nestedarray
ofstring|int
)- exceptions
MalformedPatternException
,SubjectNotMatchedException
,GroupNotMatchedException
,NonexistentGroupException
- exposes proper methods (instead of returning
T-Regx uses PHP regular expressions under the hood, but its well encapsulated.
#
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.
#
Example of exception handlingExample usage of an incorrect pattern:
Example handling of backtracking limit:
#
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.
Similar code completion is available for Matcher
, Group
and other parts of T-Regx library.
#
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.