Download the PHP package chriskonnertz/regex without Composer
On this page you can find all versions of the php package chriskonnertz/regex. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chriskonnertz/regex
More information about chriskonnertz/regex
Files in chriskonnertz/regex
Package regex
Short Description Use methods to fluently create a regular expression in PHP
License MIT
Informations about the package regex
RegEx
Use methods to fluently create a regular expression in PHP. This is more intuitive and understandable than writing plain regular expressions.
Installation
Through Composer:
From then on you may run composer update
to get the latest version of this library.
It is possible to use this library without using Composer but then it is necessary to register an autoloader function.
This library requires PHP 7.0 or higher.
Usage example
Here is an example. It assumes that there is an autoloader.
This will print out /(?:http)(?:s)?(?:\:\/\/)(?:www\.)?(?:\w+)(?:\.)(?:\w+)/
and
match for example https://www.example.org
.
There is not much beauty in this regular expression. However, it is valid.
Note that special characters will be quoted: 123-456
will become 123\-456
.
You may call the addRaw()
method to avoid this behaviour.
More examples
Here is an example how to create a nested regular expression:
This will print out [a-zA-Z]+
(with some extras).
Re-using the RegEx
object works this way:
The clear()
method will reset the RegEx
object so you can reuse it to build a new regular expression.
Builder methods
The example results might differ from the actual output. Some of them are simplified to make it easier to understand them.
addAnyChar
Adds a partial expression that expects any single character (except by default "new line").
Example of the resulting regex string: .
Examples of matching strings: a
, 1
addAnyChars
Adds a partial expression that expects 1..n of any characters (except by default "new line").
Example of the resulting regex string: .+
Examples of matching strings: a
, a1
addMaybeAnyChars
Adds a partial expression that expects 0..n of any characters (except by default "new line").
Example of the resulting regex string: .*
Examples of matching strings: a
, a1
, empty string
addDigit
Adds a partial expression that expects a single digit.
Same as: [0-9]
Example of the resulting regex string: \d
Examples of matching strings: 1
, 0
addDigits
Adds a partial expression that expects 1..n of digits.
Same as: [0-9]+
Example of the resulting regex string: \d+
Examples of matching strings: 1
, 12
addMaybeDigits
Adds a partial expression that expects 0..n of digits.
Same as: [0-9]*
Example of the resulting regex string: \d*
Examples of matching strings: 1
, 12
, empty string
addNonDigit
Adds a partial expression that expects a character that is not a digit.
Same as: [^0-9]
Example of the resulting regex string: \D
Examples of matching strings: a
, -
addNonDigits
Adds a partial expression that expects 1..n of characters that are not digits.
Same as: [^0-9]+
Example of the resulting regex string: \D+
Examples of matching strings: a
, ab
addMaybeNonDigits
Adds a partial expression that expects 0..n of characters that are not digits.
Same as: [^0-9]*
Example of the resulting regex string: \D*
Examples of matching strings: a
, ab
, empty string
addLetter
Adds a partial expression that expects a single letter.
Example of the resulting regex string: [a-zA-Z]
Examples of matching strings: a
, Z
addLetters
Adds a partial expression that expects 1..n of letters.
Example of the resulting regex string: [a-zA-Z]+
Examples of matching strings: a
, aB
addMaybeLetters
Adds a partial expression that expects 0..n of letters.
Example of the resulting regex string: [a-zA-Z]*
Examples of matching strings: a
, aB
, empty string
addWordChar
Adds a partial expression that expects a single word character.
This includes letters, digits and the underscore.
Same as: [a-zA-Z_0-9]
Example of the resulting regex string: \w
Examples of matching strings: a
, B
, 1
addWordChars
Adds a partial expression that expects 1..n of word characters.
This includes letters, digits and the underscore.
Same as: [a-zA-Z_0-9]+
Example of the resulting regex string: \w+
Examples of matching strings: a
, ab
addMaybeWordChars
Adds a partial expression that expects 0..n of word characters.
This includes letters, digits and the underscore.
Same as: [a-zA-Z_0-9]*
Example of the resulting regex string: \w*
Examples of matching strings: a
, ab
, empty string
addNonWordChar
Adds a partial expression that expects a character that is not a word character.
This includes letters, digits and the underscore.
Same as: [^a-zA-Z_0-9]
Example of the resulting regex string: \W
Example of matching string: -
addNonWordChars
Adds a partial expression that expects 1..n of characters that are not word characters.
This includes letters, digits and the underscore.
Same as: [^a-zA-Z_0-9]+
Example of the resulting regex string: \W+
Examples of matching strings: -
, -=
addMaybeNonWordChars
Adds a partial expression that expects 0..n of characters that are not word characters.
This includes letters, digits and the underscore.
Same as: [^a-zA-Z_0-9]*
Example of the resulting regex string: \W*
Examples of matching strings: -
, -=
, empty string
addWhiteSpaceChar
Adds a partial expression that expects a white space character. This includes: space, \f, \n, \r, \t and \v
Example of the resulting regex string: \s
Example of matching string:
(one space)
addWhiteSpaceChars
Adds a partial expression that expects 1..n of white space characters. This includes: space, \f, \n, \r, \t and \v
Example of the resulting regex string: \s+
Examples of matching strings: ` (one space),
` (two spaces)
addMaybeWhiteSpaceChars
Adds a partial expression that expects 0..n of white space characters. This includes: space, \f, \n, \r, \t and \v
Example of the resulting regex string: \s*
Examples of matching strings: ` (one space),
` (two spaces), empty string
addTabChar
Adds a partial expression that expects a single tabulator (tab).
Example of the resulting regex string: \t
Examples of matching strings: \t
addTabChars
Adds a partial expression that expects 1..n tabulators (tabs).
Example of the resulting regex string: \t+
Examples of matching strings: \t
, \t\t
addMaybeTabChars
Adds a partial expression that expects 0..n tabulators (tabs).
Example of the resulting regex string: \t*
Examples of matching strings: \t
, \t\t
, empty string
addLineBreak
Adds a partial expression that expects a line break.
Per default \n
and \r\n
will be recognized.
You may pass a parameter to define a specific line break pattern.
Example of the resulting regex string: \r?\n
Examples of matching strings: \n
, \r\n
addLineBreaks
Adds a partial expression that expects a 1..n line breaks.
Per default \n
and \r\n
will be recognized.
You may pass a parameter to define a specific line break pattern.
Example resulting regex: (\r?\n)+
Examples of matching strings: \n
, \n\n
addMaybeLineBreaks
Adds a partial expression that expects a 0..n line breaks.
Per default \n
and \r\n
will be recognized.
You may pass a parameter to define a specific line break pattern.
Example resulting regex: (\r?\n)*
Examples of matching strings: \n
, \n\n
, empty string
addLineBeginning
Adds a partial expression that expects the beginning of a line. Line breaks mark the beginning of a line.
Example of the resulting regex string: ^
addLineEnd
Adds a partial expression that expects the end of a line. Line breaks mark the end of a line.
Example of the resulting regex string: $
addRange
Adds one ore more ranges to the overall regular expression and wraps them in a "range" expression.
Available from-to-ranges: a-z
, A-Z
, 0-9
ATTENTION: This expression will not automatically quote its inner parts.
Example of the resulting regex string: [a-z123\-]
Examples of matching strings: a
, 1
, -
This method uses the
RangeEx
expression class.
addInvertedRange
Adds one ore more ranges to the overall regular expression and wraps them in an inverted "range" expression.
Available from-to-ranges: a-z
, A-Z
, 0-9
ATTENTION: This expression will not automatically quote its inner parts.
Example of the resulting regex string: [^a-z123\-]
Examples of matching strings: A
, 4
, =
This method uses the
RangeEx
expression class.
addAnd
Adds one ore more partial expression to the overall regular expression and wraps them in an "and" expression. This expression requires that all of its parts exist in the tested string.
Example of the resulting regex string: http
Example of matching string: http
The
RegEx
class has a constructor that is an alias of theaddAnd
method:new RegEx('ab')
will generate the same regular expression asregEx->andAdd('ab')
.This method uses the
AndEx
expression class.
addOr
Adds at least two partial expressions to the overall regular expression and wraps them in an "or" expression. This expression requires that one of its parts exists in the tested string.
Example of the resulting regex string: http|https
Examples of matching strings: http
, https
This method uses the
OrEx
expression class.
addOption
Adds one ore more partial expressions to the overall regular expression and wraps them in an "optional" expression. The parts of this expression may or may not exist in the tested string.
Example of the resulting regex string: https(s)?
Examples of matching strings: http
, https
This method uses the
OptionEx
expression class.
addRepetition
Adds one ore more partial expressions to the total regular expression and wraps them in a "repetition" expression.
Expects the minimum and the maximum of repetitions as the first two arguments.
The parts of this expression have to appear $min
to $max
times in the tested string.
This method uses the
RepetitionEx
expression class.
addCapturingGroup
Adds one ore more partial expressions to the overall regular expression and wraps them in a "capturing group" expression. This expression will be added to the matches when the overall regular expression is tested. If you add more than one part these parts are linked by "and".
Example of the resulting regex string: (test)
This method uses the
CapturingGroupEx
expression class.
addComment
Add one ore more comments to the overall regular expression and wrap them in a "comment" expression. This expression will not automatically quote its its inner parts. ATTENTION: Comments are not allowed to include any closing brackets ( ")" )! Quoting them will not work.
Example of the resulting regex string: (?#This is a comment)
Consider to use PHP comments in favor of regular expression comments.
This method uses the
CommentEx
expression class.
Miscellaneous methods
quote
Quotes (escapes) regular expression characters and returns the result.
Example: Hello.
=> Hello\.
setModifier
Activates or deactivates a modifier. The current state of the modifier does not matter, so for example you can (pseudo-)deactivate a modifier before ever activating it.
setInsensitiveModifier etc.
Activates or deactivates the "insensitive" ("i") modifier. There are setters for all modifiers.
getActiveModifiers
Returns an array with the modifier shortcuts that are currently active.
isModifierActive()
Decides if a modifier is active or not
test
Tests a given subject (a string) against the regular expression. Returns the matches. Throws an exception if an error occurs while testing.
replace
Performs search and replace with the regular expression. Returns the modified string. Throws an exception if an error occurs while replacing.
traverse
Call this method if you want to traverse it and all of it child expressions, no matter how deep they are nested in the tree. You only have to pass a closure, you do not have to pass an argument for the level parameter. The callback will have three arguments: The first is the child expression (an object of type AbstractExpression or a string | int | float), the second is the level of that expression and the third tells you if it has children.
clear
Resets the regular expression.
getSize
Returns the number of partial expressions. If the parameter is false, only the partial expressions on the root level are counted. If the parameter is true, the method traverses trough all partial expressions and counts all partial expressions without sub expressions. Or with other words: If you imagine the regular expression as a tree then this method will only count its leaves.
getExpressions
Getter for the partial expressions array.
getStart
Getter for the "start" property
setStart
Setter for the "start" property. This is a raw string.
getEnd
Getter for the "end" property. This is a raw string - it is not quoted.
setEnd
Setter for the "end" property
getVisualisation
Returns a "visualisation" of the structure of the regular expression. This might be helpful if you want to understand how the regular expression is built. If the parameter is set to true, the result may include HTML tags.
Example output:
toString
Returns the concatenated partial regular expressions as a string.
The magic method __toString
has been implemented as well so you may convert the RegEx
object to a string.
PHPVerbalExpressions
RegEx has been inspired by PHPVerbalExpressions. It is not better than VerbalExpressions but different. RegEx makes more use of OOP principles. Therefore it is better suited to mimic the structure of a regular expression. On the downside it is a little bit more complex.
General notes
-
Contributions welcome. Do not hesitate to create issues and pull requests. Let me know if you miss a method.
-
If you want to test your regular expression, you may try an online regex tester.
-
Why the extensive use of (not capturing) groups? Well, to me it is not very intuitive that "ab" is the same as "(?:ab)". Always using (non capturing) groups emphasizes the structure of a regular expression.
-
Official PHP documentation about the syntax of regular expressions: http://php.net/manual/de/reference.pcre.pattern.syntax.php
-
The code of this library is formatted according to the code style defined by the PSR-2 standard.
- Status of this repository: Maintained. Create an issue and you will get a response, usually within 48 hours.