Regular expressions are nothing more than a chain or pattern of characters themselves. They provide the foundation for pattern-matching functionality.


PHP offers two specific functions of regular expression functions, each corresponding to a certain type of regular expression. You can use any of them based on your convenience.

  • POSIX Regular Expressions
  • PERL Style Regular Expressions

POSIX Regular Expressions

The formation of a POSIX regular expression is not different to that of a typical arithmetic expression: various elements (operators) are combined to form more complex expressions.


Brackets

Brackets ([]) have special meaning when used in the context of Regular expressions. They are used to find a list of characters.

Expression Description
[0-9]It matches any decimal digit from 0 through 9.
[a-z]It matches any character from lower-case a through lowercase z.
[A-Z]It matches any character from uppercase A through uppercase Z.
[a-Z]It matches any character from lowercase a through uppercase Z.

Quantifiers

Frequency or position in brackets and brackets can be indicated by a special character. Each special letter has a specific meaning. I +, *,?, {Int. width}, and $ flags all follow alphabetical order.

Expression Description
p+It matches any string containing at least one p.
p*It matches any string containing zero or more p's.
p?It matches any string containing zero or one p's
p{N}It matches any string containing a sequence of N p's
p{2,3}It matches any string containing a sequence of two or three p's.
p{2, }It matches any string containing a sequence of at least two p's.
p$It matches any string with p at the end of it.
^pIt matches any string with p at the beginning of it.

Examples

Following examples will clear your concepts about matching characters.

Expression Description
[^a-zA-Z]It matches any string not containing any of the characters ranging from a through z and A through Z.
p.pIt matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$It matches any string containing exactly two characters.
(.*)It matches any string enclosed within and .
p(hp)*It matches any string containing a p followed by zero or more instances of the sequence php.


Predefined Character Ranges

To make it easier for you to plan your programs the predefined bullet ranges, also known as character classes, are available. Character classes specify the full range of characters, for example, alphabets or a complete set -

Expression Description
[[:alpha:]]It matches any string containing alphabetic characters aA through zZ.
[[:digit:]]It matches any string containing numerical digits 0 through 9.
[[:alnum:]]It matches any string containing alphanumeric characters aA through zZ and 0 through 9.
[[:space:]]It matches any string containing a space.

PHP's Regexp POSIX Functions

PHP currently offers seven functions for searching strings using POSIX-style regular expressions −

Function Description
ereg()

The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.

ereg_replace()

The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found.

eregi()

The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive.

eregi_replace()

The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive.

split()

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

spliti()

The spliti() function operates exactly in the same manner as its sibling split(), except that it is not case sensitive.

sql_regcase()

The sql_regcase() function can be thought of as a utility function, converting each character in the input parameter string into a bracketed expression containing two characters.


PERL Style Regular Expressions

Perl's typical style is similar to its POSIX counterparts. The POSIX syntax can be used almost invariably with Perl's standard style functions. In fact, you can use any of the quantifiers introduced in the previous POSIX section.


Meta characters

A meta character is simply an alphabetic alphabetical sequence that gives the combination a special meaning.

For example, you can search for coins using the '\ d' meta character: / ([\ d] +) 000 /, Here \ d you will search for any string of numbers.

The following is a list of meta characters that can be used in general PERL style descriptions.

Character            Description
. a                        single character
\s                         a whitespace character (space, tab, newline)
\S                        non-whitespace character
\d                        a digit (0-9)
\D                        a non-digit
\w                        a word character (a-z, A-Z, 0-9, _)
\W                       a non-word character
[aeiou]                 matches a single character in the given set
[^aeiou]               matches a single character outside the given set
(foo|bar|baz)        matches any of the alternatives specified