Skip to content
mck89 edited this page Mar 17, 2015 · 8 revisions

Simple patterns

Char

The class REBuilder\Pattern\Char represents a simple text pattern and it can be added using addChar or addCharAndContinue. This functions accept the string that will be matched in the regex:

$regex->addChar("abc");

//or

$regex->addChild(new REBuilder\Pattern\Char("abc"));

echo $regex; // "/abc/"

You can set or get the characters of the class with setChar and getChar methods.

Ideally this class should represent only one character, but to speed up the process of building the regex it accepts strings of any length and, since repetitions are supported only on single characters, if you set a repetition on a Char that contains a string longer than one character, it will be rendered as a non matching subpattern.

GenericCharType

The class REBuilder\Pattern\GenericCharType represents a generic character type like:

  • \w: word characters
  • \d: decimal digits
  • \s: whitespaces

You can find the complete list of generic character types in PHP documentation.

It can be added using addGenericCharType or addGenericCharTypeAndContinue, this functions accept the identifier of the generic char type:

$regex->addGenericCharType("w");

//or

$regex->addChild(new REBuilder\Pattern\GenericCharType("w"));

echo $regex; // "/\w/"

You can set or get the generic char type identifier using setIdentifier and getIdentifier methods.

Note that this class throws exceptions if you set an invalid identifier.

Dot

The class REBuilder\Pattern\Dot represents a dot as described in PHP documentation, that matches any character. It can be added using addDot or addDotAndContinue:

$regex->addDot();

//or

$regex->addChild(new REBuilder\Pattern\Dot());

echo $regex; // "/./"

In UTF-8 mode ("u" modifier) if you want to match a single byte you can use the REBuilder\Pattern\Byte class with addByte or addByteAndContinue.

HexChar

The class REBuilder\Pattern\HexChar represents a character whose code number is defined by two hexadecimal digits. It can be added using addHexChar or addHexCharAndContinue, this functions accept the hexadecimal number that identifies the character:

$regex->addHexChar("A0");

//or

$regex->addChild(new REBuilder\Pattern\HexChar("A0"));

echo $regex; // "/\xA0/"

You can set or get the hexadecimal code using setChar and getChar methods.

Note that this class throws exceptions if you set an invalid hexadecimal number or if it's longer than 2 characters.

OctalChar

The class REBuilder\Pattern\OctalChar represents a character whose code number is defined by three octal digits. It can be added using addOctalChar or addOctalCharAndContinue, this functions accept the octal number that identifies the character:

$regex->addOctalChar("022");

//or

$regex->addChild(new REBuilder\Pattern\OctalChar("011"));

echo $regex; // "/\011/"

You can set or get the octal code using setChar and getChar methods.

Note that this class throws exceptions if you set an invalid octal number or if it's longer than 3 characters.

ControlChar

The class REBuilder\Pattern\ControlChar represents a control character (to better understand its behaviour refer to the PHP cocumentation). It can be added using addControlChar or addControlCharAndContinue, this functions accept a character:

$regex->addControlChar(";");

//or

$regex->addChild(new REBuilder\Pattern\ControlChar(";"));

echo $regex; // "/\c;/"

You can set or get the character using setChar and getChar methods.

NonPrintingChar

The class REBuilder\Pattern\NonPrintingChar can be used for characters that can't be represented in a visible manner, like:

  • \n: newline
  • \e: escape
  • \r: carriage return

You can find the complete list of non printing characters in PHP documentation.

It can be added using addNonPrintingChar or addNonPrintingCharAndContinue, this functions accept the identifier of the non printing character:

$regex->addNonPrintingChar("n");

//or

$regex->addChild(new REBuilder\Pattern\NonPrintingChar("n"));

echo $regex; // "/\n/"

You can set or get the non printing character identifier using setIdentifier and getIdentifier methods.

Note that this class throws exceptions if you set an invalid identifier.

Clone this wiki locally