verbalexp

Constructing hard regular expressions with VerbalExpressions

Most newbie (and some seasoned) programmers have difficultly constructing Regular Expressions. Many a times one needs to create a Regexp quickly to test a a particular piece of code. However, not being comfortable withe Regexps can be a problem. VerbalExpressions is a PHP library that enables you to construct regular expressions using natural language like constructs. Think of it like a DSL for building Regexps.

Below is a sample PHP code that constructs a regular expression which tests whether a given string is a valid url.

<?php
 
include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
 
if($regex->test("http://www.codediesel.com"))
    echo "valid url";
else
    echo "invalid url";
 
?>

The main part of the code the DSL like interface that helps you build a Regexp.

$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();

If you want to see what regular expression the code has built, we can use the getRegex function of the class.

<?php
 
include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
echo $regex->getRegex();

This will print the Regexp given below.

/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m

We can now use the above Regexp in our code to accomplish the same thing as above.

$myRegexp = '/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m';
 
if (preg_match($myRegexp, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}

We can also use the $regex object given in the above example directly in our code where the particular Regexp is required.

include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
if (preg_match($regex, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}

The PHP version of VerbalExpressions is a port of the original JavaScript version, JSVerbalExpressions. Additional modifiers that will help you build regular expressions can be found here.



3 thoughts on “Constructing hard regular expressions with VerbalExpressions

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>