Skip to content
Max Girkens edited this page Feb 26, 2015 · 23 revisions

Docs

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.


RegExpBuilder

$builder = new RegExpBuilder();

All public methods except getRegExp() are chainable.

find(string)

Alias for then, sounds nicer at the beginning.

$builder
    ->find("Hey, ")
    ->then("where do you go to get ")
    ->eitherFind("juice")
    ->orFind("milk");

related: then()

then(string)

Matches a single occurance of the string.

$builder
    ->startOfLine()
    ->then("Where do you go to get ")
    ->eitherFind("juice")
    ->orFind("milk");

some(array)

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(".");

maybeSome(array)

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);

maybe(string)

Specifies that the string may or may not occur.

$builder
    ->maybe("milk");

anything()

Specifies that there is zero or more of any character.

$builder
    ->anything();

anythingBut(string)

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");

something()

Specifies that there is one or more of any character.

$builder
    ->something();

lineBreak()

Specifies that there is a line break.

$builder
    ->lineBreak();

lineBreaks()

Specifies that there is a line break.

$builder
    ->exactly(2)
    ->lineBreaks();

whitespace()

If used alone, specifies that there is a single whitespace character.

$builder
  ->whitespace();

tab()

Specifies that there is a single tab.

$builder
    ->tab();

tabs()

Specifies that there is a tab.

$builder
    ->exactly(2)
    ->tabs();

digit()

Specifies that there is a single digit.

$builder
    ->digit();

digits()

Specifies that there is a digit.

$builder
    ->exactly(2)
    ->digits();

letter()

Specifies that there is a single letter.

$builder
    ->letter();

letters()

Specifies that there is a letter.

$builder
  ->exactly(2)
  ->letters();

lowerCaseLetter()

Specifies that there is a single lowercase letter.

$builder
  ->lowerCaseLetter();

lowerCaseLetters()

Specifies that there is a lowercase letter.

$builder
  ->exactly(2)
  ->lowerCaseLetters();

upperCaseLetter()

Specifies that there is a single uppercase letter.

$builder
  ->upperCaseLetter();

upperCaseLetters()

Specifies that there is an uppercase letter.

$builder
  ->exactly(2)
  ->upperCaseLetters();

startOfInput()

Matches the start of input.

$builder
  ->startOfInput()
  ->min(1)
  ->of("milk");

related: endOfInput()

startOfLine()

Matches the start of input or start of a line.

$builder
  ->startOfLine()
  ->min(1)
  ->of("milk");

related: endOfLine()

endOfInput()

Matches the end of input.

$builder
  ->min(1)
  ->of("milk")
  ->endOfInput();

related: startOfInput()

endOfLine()

Matches the end of input or line.

$builder
  ->min(1)
  ->of("milk")
  ->endOfLine();

related: startOfLine()

eitherFind(RegExpBuilder)

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)

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)

eitherFind(string)

Used with or. This method takes a string.

$builder
  ->eitherFind("milk")
  ->orFind("juice");

related: orFind(string)

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)

neither(RegExpBuilder)

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)

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)

neither(string)

Used with nor. This method takes a string.

$builder
  ->neither("milk")
  ->nor("juice");

related: nor(string)

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)

exactly(number)

Used to specify the exact number of times a pattern will repeat.

$builder
  ->exactly(1)
  ->of("milk");

related: min(number), max(number)

min(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)

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)

of(string)

Used to match a string.

$builder
  ->exactly(1)
  ->of("milk");

related: from(array), notFrom(array), like(RegExpBuilder)

anyOf(string)

Match one of multiple strings.

$builder
    ->startOfInput()
    ->min(2)->max(5)->letters
    ->anyOf(array(".jpg", ".gif", ".png"));

related: from(array), notFrom(array))

ofAny()

Used to match any character.

$builder
  ->exactly(1)
  ->ofAny();

related: anything()

ofGroup(number)

Used to match a previously defined group.

$builder
  ->exactly(3)
  ->of("milk")
  ->asGroup()
  ->exactly(1)
  ->of("juice")
  ->exactly(1)
  ->ofGroup(1);

related: asGroup()

from(array)

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)

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)

like(RegExpBuilder)

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);

reluctantly()

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();

ahead(RegExpBuilder)

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)

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)

asGroup()

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)

ignoreCase()

Used before calling getRegExp() to ensure that the regular expression ignores case.

$builder
  ->min(1)
  ->max(3)
  ->of("Milk")
  ->ignoreCase()
  ->getRegExp();

multiLine()

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();

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->test("milk"); //true

append(RegExpBuilder)

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);

optional(RegExpBuilder)

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);

RegExp

A RegExp Object is returned by the getRegExp() method of the builder.

$regExp = $builder->getRegExp();

matches(string)

Test string against current pattern

$regExp = $builder
    ->find("abc")
    ->getRegExp();

$regExp->matches("abc"); //true

findIn(string)

Execute preg_match return matches

$regExp = $builder
    ->find("abc")
    ->getRegExp();

$matches = $regExp->findIn("abc");

exec(string)

alias for findIn()

replace(callable)

Replace matches for current pattern

$regExp = $builder
    ->find("abc")
    ->getRegExp();

$matches = $regExp->replace("abcdef", function($match){
    return strtoupper($match);
});

// "ABCdef"
You can’t perform that action at this time.