2. Brackets
• Regular expressions provide the foundation for describing or matching data
according to defined syntax rules.
• There are three methods that POSIX supports for locating different
character sequences: brackets, quantifiers, and predefined character
ranges.
• Brackets ([]) are used to represent a list, or range, of characters to be
matched.
• For instance, the regular expression [php] will find any string containing
the character p or h. Several commonly used character ranges follow:
• [0-9] matches any decimal digit from 0 through 9.
• [a-z] matches any character from lowercase a through lowercase z.
• [A-Z] matches any character from uppercase A through uppercase Z.
• [A-Za-z] matches any character from uppercase A through lowercase z.
3. Quantifiers
• Here are several examples of these characters:
• p+ matches any string containing at least one p.
• p* matches any string containing zero or more p’s.
• p? matches any string containing zero or one p.
• p{2} matches any string containing a sequence of two p’s.
• p{2,3} matches any string containing a sequence of two or three p’s.
• p{2,} matches any string containing a sequence of at least two p’s.
• p$ matches any string with p at the end of it.
• Still other flags can be inserted before and within a character sequence:
• ^p matches any string with p at the beginning of it.
• [^a-zA-Z] matches any string not containing any of the characters ranging from
a through z and A through Z.
• p.p matches any string containing p, followed by any character, in turn
followed by another p.
• You can also combine special characters to form more complex expressions.
Consider the following examples:
• ^.{2}$ matches any string containing exactly two characters.
• <b>(.*)</b> matches any string enclosed within <b> and </b>.
• p(hp)* matches any string containing a p followed by zero or more
instances of the sequence hp.
4. Predefined Character Ranges (Character Classes)
• Several predefined character ranges, also known as character classes, are
available.
• Character classes specify an entire range of characters—for example, the
alphabet or an integer set.
• Standard classes include the following:
• [:alpha:]: Lowercase and uppercase alphabetical characters. This can also be
specified as [A-Za-z].
• [:alnum:]: Lowercase and uppercase alphabetical characters and numerical
digits. This can also be specified as [A-Za-z0-9].
• [:cntrl:]: Control characters such as tab, escape, or backspace.
• [:digit:]: Numerical digits 0 through 9. This can also be specified as [0-9].
• [:graph:]: Printable characters found in the range of ASCII 33 to 126.
• [:lower:]: Lowercase alphabetical characters. This can also be specified as [a-z].
• [:punct:]: Punctuation characters, including ~ ` ! @ # $ % ^ & * ( ) - _ + = { } [
] : ; ' < > , . ? and /.
• [:upper:]: Uppercase alphabetical characters. This can also be specified as [A-
Z].
• [:space:]: Whitespace characters, including the space, horizontal tab,
vertical tab, new line, form feed, or carriage return.
• [:xdigit:]: Hexadecimal characters. This can also be specified as [a-fA-F0-9].
5. Performing a Case-Sensitive Search
• The ereg() function executes a case-sensitive search of a string for a
defined pattern, returning the length of the matched string if the pattern is
found and FALSE otherwise. Its prototype follows:
• int ereg(string pattern, string string [, array regs])
6. Performing a Case-Insensitive Search
• The eregi() function searches a string for a defined pattern in a case-
insensitive fashion. Its prototype follows:
• int eregi(string pattern, string string, [array regs])
7. Replacing Text in a Case-Sensitive Fashion
• The ereg_replace() function operates much like ereg(), except that its
power is extended to finding and replacing a pattern with a replacement
string instead of simply locating it. Its prototype follows:
• string ereg_replace(string pattern, string replacement, string string)
8. Replacing Text in a Case-Insensitive Fashion
• The eregi_replace() function operates exactly like ereg_replace(), except
that the search for pattern in string is not case sensitive. Its prototype
follows:
• string eregi_replace(string pattern, string replacement, string string)
WRITE PHP CODE IMPLEMENTING THIS
9. Splitting a String into Various Elements Based
on a Case-Sensitive Pattern
• The split() function divides a string into various elements, with the
boundaries of each element based on the occurrence of a defined pattern
within the string. Its prototype follows:
• array split(string pattern, string string [, int limit])
• The optional input parameter limit is used to specify the number of
elements into which the string should be divided, starting from the left end
of the string and working rightward. In cases where the pattern is an
alphabetical character, split() is case sensitive.
10. Splitting a String into Various Elements Based
on a Case-Insensitive Pattern
• The spliti() function operates exactly in the same manner as its sibling,
split(), except that its pattern is treated in a case-insensitive fashion. Its
prototype follows:
• array spliti(string pattern, string string [, int limit])
WRITE PHP CODE IMPLEMENTING THIS
11. Accommodating Products Supporting Solely
Case-Sensitive Regular Expressions
• The sql_regcase() function converts each character in a string into a
bracketed expression containing two characters. If the character is
alphabetical, the bracket will contain both forms; otherwise, the original
character will be left unchanged. Its prototype follows:
• string sql_regcase(string string)