What's T-Regx?
T-Regx (T-Rex and RegExp) is a lightweight, high-level Regular Expressions library.
Its main features are:
Standard or new functions:
- You can stay with known
preg_match()
,preg_replace()
functions (but safe) - You can use the new
pattern()
function - You can use either; you can use both; you can interchange them
- You can stay with known
API:
- Automatic delimiters for your patterns
- UTF-8 support out of the box
- Unifying differences between matching, replacing, splitting into one interface:
Detail
- Prepared Patterns handling unsafe characters (e.g. user input)
Removing headaches of PHP:
You only use
try
/catch
.T-Regx is doing all the complex
if
ology aroundpreg
under the hood, to unify all PHP corner-cases and map them to exceptions (see "Comparison").All false positives and false negatives are handled
If anything - Exceptions!:
- Magic values (like
null
,false
,''
,[]
,-1
) aren't used -> Exceptions - Handles every
warning
/error
/fatal error
/notice
-> Exception preg_last_error()
isn't required to verify errors -> Exceptionerrors
/bugs/gotcha's
aren't silently ignored -> Exception- supplied invalid arguments aren't silently ignored -> Exception
- returned invalid values aren't silently ignored -> Exception
- Magic values (like
Additionally:
- Each function obeys SRP
- Functionalities are represented with
methods()
(notFLAGS
or arguments) - No default
parameters=null
- No
PREG_FLAGS, 1
- No
varargs...
#
PHP's magic valuesIn vanilla PHP, different methods (preg_match()
, preg_replace()
, preg_split()
, etc.) return different types and
values, that are symbolic. For some it's null
or ""
, for other []
or false
, 0
for others it's -1
- they're all
supposed to mean "error". For example, preg_match()
returns false
on error, but preg_replace()
returns null
on error.
Of course, you have to remember which is === false
and which === null
.
T-Regx will never return null
, false
an empty array or other magic value. MalformedPatternException
is thrown if $pattern
is malformed.
And it's not just the return values, $match
results also contain magic values like ""
which can either mean
the group wasn't matched, or it matched an empty string in PHP. In SafeRegex the false negative is left as it is, we don't change
the API to allow simple migration of preg_match()
to preg::match()
.
In CleanRegex this is handled, so you don't have to worry about it - ""
always means an empty string.