-
Notifications
You must be signed in to change notification settings - Fork 50
Home
RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.
$builder = new RegExpBuilder();
All public methods except getRegExp() are chainable.
Alias for then, sounds nicer at the beginning.
$builder
->find("Hey, ")
->then("where do you go to get ")
->eitherFind("juice")
->orFind("milk");
related: then()
Matches a single occurance of the string.
$builder
->startOfLine()
->then("Where do you go to get ")
->eitherFind("juice")
->orFind("milk");
Specifies that there is at least one character and that each character must be one of those contained in the array of strings. Each string in the array must be a single character.
$digits = array("3", "7", "8");
$builder
->some($digits)
->then(".");
The same as ->some(array), except that there may be no characters at all, rather than at least one.
$digits = array("3", "7", "8");
$builder
->maybeSome($digits);
Specifies that the string may or may not occur.
$builder
->maybe("milk");
Specifies that there is zero or more of any character.
$builder
->anything();
Specifies that there is zero or more of any character as long as the string of resulting characters does not start with the supplied string.
$builder
->anythingBut("milk");
Specifies that there is one or more of any character.
$builder
->something();
Specifies that there is a line break.
$builder
->lineBreak();
Specifies that there is a line break.
$builder
->exactly(2)
->lineBreaks();
If used alone, specifies that there is a single whitespace character.
$builder
->whitespace();
Specifies that there is a single tab.
$builder
->tab();
Specifies that there is a tab.
$builder
->exactly(2)
->tabs();
Specifies that there is a single digit.
$builder
->digit();
Specifies that there is a digit.
$builder
->exactly(2)
->digits();
Specifies that there is a single letter.
$builder
->letter();
Specifies that there is a letter.
$builder
->exactly(2)
->letters();
Specifies that there is a single lowercase letter.
$builder
->lowerCaseLetter();
Specifies that there is a lowercase letter.
$builder
->exactly(2)
->lowerCaseLetters();
Specifies that there is a single uppercase letter.
$builder
->upperCaseLetter();
Specifies that there is an uppercase letter.
$builder
->exactly(2)
->upperCaseLetters();
Matches the start of input.
$builder
->startOfInput()
->min(1)
->of("milk");
related: endOfInput()
Matches the start of input or start of a line.
$builder
->startOfLine()
->min(1)
->of("milk");
related: endOfLine()
Matches the end of input.
$builder
->min(1)
->of("milk")
->endOfInput();
related: startOfInput()
Matches the end of input or line.
$builder
->min(1)
->of("milk")
->endOfLine();
related: startOfLine()
Used with or. This method takes a new RegExpBuilder object.
$a = $builder->exactly(1)->of("milk");
$b = $builder->exactly(2)->of("juice");
$builder
->eitherFind($a)
->orFind($b);
related: orFind(RegExpBuilder)
Used after either. Multiple or methods can be chained together. This method takes a new RegExpBuilder object.
$a = $builder->exactly(1)->of("milk");
$b = $builder->exactly(2)->of("juice");
$builder
->eitherFind($a)
->orFind($b);
related: eitherFind(RegExpBuilder)
Used with or. This method takes a string.
$builder
->eitherFind("milk")
->orFind("juice");
related: orFind(string)
Used after either. Multiple or methods can be chained together. This method takes a string.
$builder
->eitherFind("milk")
->orFind("juice");
related: eitherFind(string)
Used with nor. This method takes a new RegExpBuilder object.
$a = $builder->exactly(1)
->of("milk");
$b = $builder->exactly(2)
->of("juice");
$builder
->neither(p1)
->nor(p2);
related: nor(RegExpBuilder)
Used after neither. Multiple nor methods can be chained together. This method takes a new RegExpBuilder object.
$a = $builder->exactly(1)
->of("milk");
$b = $builder->exactly(2)
->of("juice");
$builder
->neither(p1)
->nor(p2);
related: neither(RegExpBuilder)
Used with nor. This method takes a string.
$builder
->neither("milk")
->nor("juice");
related: nor(string)
Used after neither. Multiple nor methods can be chained together. This method takes a string.
$builder
->neither("milk")
->nor("juice");
related: neither(string)
Used to specify the exact number of times a pattern will repeat.
$builder
->exactly(1)
->of("milk");
related: min(number), max(number)
Used to specify the minimum number of times a pattern will repeat. This method can optionally occur before max()
$builder
->min(1)
->of("milk");
related: max(number)
Used to specify the maximum number of times a pattern will repeat. This method can optionally occur after min()
$builder
->max(1)
->of("milk");
related: min(number)
Used to match a string.
$builder
->exactly(1)
->of("milk");
related: from(array), notFrom(array), like(RegExpBuilder)
Match one of multiple strings.
$builder
->startOfInput()
->min(2)->max(5)->letters
->anyOf(array(".jpg", ".gif", ".png"));
related: from(array), notFrom(array))
Used to match any character.
$builder
->exactly(1)
->ofAny();
related: anything()
Used to match a previously defined group.
$builder
->exactly(3)
->of("milk")
->asGroup()
->exactly(1)
->of("juice")
->exactly(1)
->ofGroup(1);
related: asGroup()
Used to match any of the characters contained in the array. It is equivalent to the regular expression [array].
$builder
->exactly(3)
->from(["p", "q", "r"]);
related: notFrom(array)
Used to match any characters other than those contained in the array. It is equivalent to the regular expression [^array].
$builder
->exactly(3)
->notFrom(["p", "q", "r"]);
related: from(array)
Used to nest patterns. This method takes a new RegExpBuilder object.
$pattern = $builder->
->min(1)
->of("milk")
->min(2)
->of("juice");
$builder
->getNew()
->exactly(2)
->like($pattern);
Used to specify that the pattern is matched reluctantly. This means that the smallest possible match is found, rather than the largest possible match.
$builder
->find("milk")
->anything()
->reluctantly()
->digit();
Equivalent to the concept of lookaheads in regular expressions. This method takes a new RegExpBuilder object.
$pattern = $builder
->exactly(1)
->of("juice");
$builder
->getNew()
->exactly(1)
->of("fruit")
->ahead($pattern);
related: notAhead(RegExpBuilder)
Equivalent to the concept of lookaheads in regular expressions. This method takes a new RegExpBuilder object.
$pattern = $builder
->exactly(1)
->of("pqr");
$builder
->getNew()
->exactly(1)
->of("milk")
->notAhead(pattern);
related: ahead(RegExpBuilder)
Equivalent to the concept of capturing groups in regular expressions.
$regExp = $builder
->min(1)
->max(3)
->of("p")
->exactly(1)
->of("milk")
->asGroup()
->exactly(1)
->from(["p", "q", "r"])
->getRegExp();
$matches = $regExp->exec("pmilkq");
$matches[1] === "milk"; //true
related: ofGroup(number)
Used before calling getRegExp() to ensure that the regular expression ignores case.
$builder
->min(1)
->max(3)
->of("Milk")
->ignoreCase()
->getRegExp();
Used before calling getRegExp() to set multiline on the regular expression. Normally when start() or end() are called, they refer to the start or end of the input. When multiLine() is called, they refer to the start or end of a line instead.
$builderA
->startOfLine()
->min(1)
->of("milk");
$builderB
->min(1)
->like($builderA)
->multiLine()
->getRegExp();
Once you have finished building your regular expression, getRegExp() should be called to get the actual RegExp object.
$regExp = $builder
->min(1)
->of("milk")
->multiLine()
->getRegExp();
$regExp->matches("milk"); //true
Used to append patterns to the end of an existing pattern.
$a = $builder
->min(1)
->of("fruit");
$b = $builder
->min(2)
->of("juice");
$a->append($b);
The regular expression in the example below suggests that p1 must occur once, and then p2 might occur once.
$a = $builder
->min(1)
->of("milk");
$b = $builder
->min(2)
->of("juice");
$a->optional($b);
A RegExp Object is returned by the getRegExp() method of the builder.
$regExp = $builder->getRegExp();
Test string against current pattern
$regExp = $builder
->find("abc")
->getRegExp();
$regExp->matches("abc"); //true
Execute preg_match return matches
$regExp = $builder
->find("abc")
->getRegExp();
$matches = $regExp->findIn("abc");
alias for findIn()
Replace matches for current pattern
$regExp = $builder
->find("abc")
->getRegExp();
$matches = $regExp->replace("abcdef", function($match){
return strtoupper($match);
});
// "ABCdef"
RegExpBuilder
ahead(RegExpBuilder)
anyOf(string)
anything()
anythingBut(string)
append(RegExpBuilder)
asGroup()
digit()
digits()
eitherFind(RegExpBuilder)
eitherFind(string)
endOfInput()
endOfLine()
exactly(number)
find(string)
from(array)
getRegExp()
ignoreCase()
letter()
letters()
like(RegExpBuilder)
lineBreak()
lineBreaks()
lowerCaseLetter()
lowerCaseLetters()
max(number)
maybe(string)
maybeSome(array)
min(number)
multiLine()
neither(RegExpBuilder)
neither(string)
nor(RegExpBuilder)
nor(string)
notAhead(RegExpBuilder)
notFrom(array)
of(string)
ofAny()
ofGroup(number)
optional(RegExpBuilder)
orFind(RegExpBuilder)
orFind(string)
reluctantly()
some(array)
something()
startOfInput()
startOfLine()
tab()
tabs()
then(string)
upperCaseLetter()
upperCaseLetters()
whitespace()
RegExp
exec(string)
findIn(string)
matches(string)
replace(callable)