codekandis / regular-expressions
`codekandis/regular-expressions` is a library to provide regular expression interfaces and classes.
Requires
- php: >=8.3
Requires (Dev)
- codekandis/phpunit: ^5.0.0
- rector/rector: ^1.0.5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-05-16 23:10:07 UTC
README
codekandis/regular-expressions
is a library to provide regular expression interfaces and classes.
Index
Installation
Install the latest version with
$ composer require codekandis/regular-expressions
How to use
RegularExpression
wraps several PHP RegEx functions. The description to the paramters used can be read in the PHP manual linked in the description of the respective methods. The parameter [$throwNoMatchException
][] is described below
Instanziation
The pattern must be passed to the constructor of the RegularExpression
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' );
If the passed pattern is invalid an InvalidRegularExpressionException
will be thrown.
RegularExpression::match()
RegularExpression::match()
wraps PHP's function preg_match()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' ); $match = $regularExpression->match( 'foo01234_bar56789' ); /** * $match = [ * 'foo01234' * ] */
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns null
.
Passing an invalid offset throws an InvalidOffsetException
.
RegularExpression::matchAll()
RegularExpression::matchAll()
wraps PHP's function preg_match_all()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' ); $matches = $regularExpression->matchAll( 'foo01234_bar56789' ); /** * $matches = [ * [ * 'foo01234', * 'bar56789' * ] * ] */
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns null
.
Passing an invalid offset throws an InvalidOffsetException
.
RegularExpression::replace()
RegularExpression::replace()
wraps PHP's function preg_replace()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' ); $replacedSubject = $regularExpression->replace( 'foo01234_bar56789', 'replacement' ); /** * $replacedSubject = 'replacement_replacement' */
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns the passed subject instead.
Passing an invalid limit throws an InvalidLimitException
.