Posted by: phillipnb | March 19, 2011

PHP and Regular Expressions – Part 2


In the last article we discussed a few rules about how to use regular expression for pattern matching. In this article we will work on a few examples to demonstrate our understanding of these rules. First, we will write a function whose job is to search through a piece of text for a particular pattern and display it if it is found. Here is the code for that function:

function findMyPattern($pattern, $subject, $matches)<br/>
{
    if (preg_match_all ($pattern, $subject, $matches))<br/>
    {
        print_r($matches);
    }
    else
    {
        echo "\n No match was found";
    }
} //

Let us play with a few examples:

Example 1:

$subject = "adcdacababcabbabbcadcbabbbcabbbbbcacefaba55e";
$pattern = "/ab?c/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example, $subject is a variable which holds the text that need to be searched, $pattern holds the regular expression pattern that we want to find, $matches is an array which will contain the matching strings and findMyPattern is the function that we will use to do the job of matching our pattern and displaying the output. In the above example, the pattern /ab?c/ means match any string which consist of ‘abc’ in a sequence, where ‘b?’ means ‘b’ has to occur zero or one time. If we run the above code, we get the following output.

Output:

Array
(
    [0] => Array
        (
            [0] => ac
            [1] => abc
            [2] => ac
        )
)

Example 2

$subject = "adcdacababcabbabbcadcbabbbcabbbbbcacefaba55e";
$pattern = "/ab{2,3}c/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example the pattern that we want to find is “/ab{2,3}c/”. This means that the string matched should contain abc with ‘b’ occurring a minimum of 2 times and a maximum of 3 times.

Output:

Array
(
    [0] => Array
        (
            [0] => abbc
            [1] => abbbc
        )
)

 

Example 3

$subject = "adcdacababcabbabbcadcbabbbcabbbbbcacefaba55e";
$pattern = "/ab*c/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example, we are trying to find a pattern which is of the form ‘abc’ with ‘b’ occurring zero or more times.

Output:

Array
(
    [0] => Array
        (
            [0] => ac
            [1] => abc
            [2] => abbc
            [3] => abbbc
            [4] => abbbbbc
            [5] => ac
        )
)

 

Example 4

$subject = "adcdacababcabbabbcadcbabbbcabbbbbcacefaba55e";
$pattern = "/ab+c/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example we are trying to match a pattern which is of the form ‘abc’ with ‘b’ occurring one or more times.

Output:

Array
(
    [0] => Array
        (
            [0] => abc
            [1] => abbc
            [2] => abbbc
            [3] => abbbbbc
        )
)

 

Example 5

$subject = "abebcabdeaccacdeaccdadea5fecfe7f";
$pattern = "/a[bc]d/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

Here, we are searching for a pattern of the form ‘abd’ or ‘acd’.

Output:

Array
(
    [0] => Array
        (
            [0] => abd
            [1] => acd
        )
)

 

Example 6

$subject = "abebcabdeaccacdeaccdadea5fecfe7f";
$pattern = "/a[b-d]e/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example, ‘b-d’ in the pattern means a range,i.e. any character between ‘b’, ‘d’ including ‘b’ and ‘d’.

Output:

Array
(
    [0] => Array
        (
            [0] => abe
            [1] => ade
        )
)

 

Example 7

$subject = "abebcabdeaccacdeaccdadea5fecfe7f";
$pattern = "/e[a-c\d]f/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

In this example we are looking for a pattern of the form which starts with an ‘e’ and ends ith ‘f’ with a character from ‘a’ to ‘c’ followed by a digit in between.

Output:

Array
(
    [0] => Array
        (
            [0] => ecf
            [1] => e7f
        )
)

We will work on more examples in the next edition of our series on regular expressions.

Till then, Happy PHPing

Advertisements

Responses

  1. […] from the last edition let us work on a few more examples: Example […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: