Regular Expressions

We could spend an entire week learning Regular Expressions (RegEx). It is an extensive topic. I thought you should be familiar with the concept, so this is a simplified primer on RegEx.

RegEx are used for pattern matching. This is helpful when searching for substrings within strings or verifying user input is of a proper format.

A Pattern is whatever you are searching for. If you are looking for the name 'Smith', then Smith is the pattern. If you are looking for the number 12 followed by a closed parenthesis, then 12) is the pattern. The pattern can contain any character the computer can generate.

There are characters that hold special meaning in RegEx. They are:

* . ? + [ ] ( ) { } ^ $ | \

Since they hold special meaning, if you are searching for one of these characters, you have to escape it. This is done by preceeding the character with a backslash '\'. (Now you know why the backslash is one of the special characters.) For instance, if you wanted to search for '...USAF is responsible', then the pattern becomes:

\.\.\.USAF is responsible

Now the PHP parser knows you are searching for actual periods, in fact, three of them in a row.

You can use character classes to aid in your search. A class replaces one of the characters in your pattern with a series of choices. A class is identified with square brackets '[ ]'. (Now you know why the square brackets are two of the special characters.)

For instance, suppose you wanted to search for the words 'woman' or 'women'. Your pattern would be: wom[ae]n. The class [ae] tells the parser to look for either of these letters at this spot in the pattern.

The character class can search for everything except what is between the brackets by using a caret ' ^ '. If the next character in your search pattern had to be anything but a 0 or a 5, then your character class would be: [^05]

You can indicate a range of characters within your class by using a hyphen ' - '. [a-z] is a class of all lower-case letters of the alphabet from a to z. [^0-9] is a class of anything that is not a number. [a-zA-Z] is a class of any english letter, both uppercase and lowercase.

An anchor is a character that identifies the beginning or end of a string.

To identify a pattern at the beginning of a string, use a caret ' ^ ' again. Note the different uses of the caret outside a class and inside it. To identify a pattern at the end of a string, use a dollar sign ' $ '. Examples of each: ^G will find every string that begins with a capital 'G'. /JA$ will find every string that ends in '/JA'.

To match either one pattern or another, use ' | '. excellent|good will find either 'excellent' or 'good'.

The wild card character is a period ' . '. me.t will find 'meat', 'meet', 'melt', 'me4t', 'me%t' and so on.

An optional pattern is identified with a question mark ' ? ' that immediately follows the optional pattern. Mrs? will find both 'Mr' and 'Mrs', since the 's' is optional. index[0-9]? will find 'index', 'index1', 'index2' and so on.

To match a pattern one or more times, use a plus sign ' + '. to+ will find 'to', 'too' and 'tooo' if it existed.

To match a pattern zero or more times, use an asterisk ' * '. Ho(ho)* will find 'Ho', 'Hoho', 'Hohoho' and so on.

To match a pattern a fixed number of times, use curly braces ' { } '. [0-9]{4} will find exactly four digits side-by-side. [0-9]{1,2} will find either a single digit, or two digits side-by-side. [0-9]{,3} will find three or fewer digits side-by-side. [0-9]{4,} will find four or more digits side-by-side.

RegEx Functions

ereg('pattern',$string)

Returns TRUE if a match for pattern was found in $string, or FALSE if no matches were found or an error occurred.
$year = 2000; if (ereg('^[0-9]{4}$',$year)) {echo $year." is a valid year";}
Will print "2000 is a valid year".

eregi('pattern',$string)

This function is identical to ereg() except that this ignores case distinction when matching alphabetic characters.
$string = "baffle"; if (eregi("f", $string)) { echo "'$string' contains a 'f' or 'F'"; }
Will produce "baffle contains a 'f' or 'F'".

ereg_replace('pattern',$replace,$string)

This function scans $string for matches to pattern, then replaces the matched text with $replace and returns the modified $string.
$string = "Hello world2"; $replace = "";
$newS = ereg_replace('[0-9]',$replace,$string); echo $newS;

Will produce "Hello world".

eregi_replace('pattern',$replace,$string)

This function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.
$string = "I am done"; $replace = "a";
$newS = eregi_replace('[aeiou]',$replace,$string); echo $newS;

Will produce "a am dana".

split('pattern',$string)

Returns an array of strings, each of which is a substring of $string formed by splitting it on boundaries formed by the case-sensitive pattern.
$myArray = split(', ','one, two, three'); print_r($myArray);
produces 'Array ( [0] => one [1] => two [2] => three )'

spliti('pattern',$string)

This function is identical to split() except that this ignores case distinction when matching alphabetic characters.
$myArray = spliti('[a-z]+','alpha7roGEr45Vt908'); print_r($myArray);
produces 'Array ( [0] => [1] => 7 [2] => 45 [3] => 908 )'

Debugging Code   < <  PREVIOUS   Table of Contents NEXT  > >   Sessions

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,514 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP11.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 10:01 pm, UTC
This file took 0.03315 seconds to process.