PHP Classes

PHP Regex Builder: Generate regular expression strings from rules

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 190 All time: 8,596 This week: 673Up
Version License PHP version Categories
regexp-r 1.0.0MIT/X Consortium ...5PHP 5, Text processing
Description 

Author

This class can generate regular expression strings from rules.

It provides several types of functions that can compose a regular expression with the rules that define the pattern of each part of the strings to build.

The class can add patterns that match an arbitrary text, the beginning or end of a string, list of characters to match or not match, pattern repetition, etc..

Each function can return strings for the regular expression and the functions can be nested to compose complex regular expressions.

Picture of Patrick Van Bergen
  Performance   Level  
Innovation award
Innovation award
Nominee: 1x

 

Example

<?php

require_once __DIR__ . '/R.php';

// Full string match
//
// /^Paradise Lost$/
//
echo R::expression()
    ->
startOfString()
    ->
text('Paradise Lost')
    ->
endOfString() . "\n";

// Alternate texts
//
// /The (dog|cat)?basket fell off the roof/
//
echo R::expression()
    ->
text('The ')
    ->
group(
       
R::group()->optional()->oneOfThese()->text('dog')->text('cat')
    )
    ->
text('basket fell off the roof') . "\n";

// Characters
//
// /the [bc]?old man and the [^c]ee.*/
//
echo R::expression()
    ->
text('the ')
    ->
inChars(R::chars('bc')->optional())
    ->
text('old man and the ')
    ->
notInChars(R::chars('c'))
    ->
text('ee')
    ->
char(R::anyChar()->zeroOrMore()) . "\n";

// Nested groups
//
// /(<a href='([^']*)'>)/
echo R::expression()
    ->
group(
       
R::group()
            ->
text("<a href='")
            ->
group(
               
R::group()
                    ->
notInChars(R::chars("'")->zeroOrMore())
            )
            ->
text("'>")
    ) .
"\n";

// Assertions
//
// /\bkettle\b/
//
echo R::expression()
    ->
wordBoundary()
    ->
text('kettle')
    ->
wordBoundary() . "\n";

// Quantifiers: Dutch postal code
//
// /[\d]{4}[a-z]{2}/
//
echo R::expression()
    ->
char(R::chars()->digit()->times(4))
    ->
char(R::chars()->letter()->times(2)) . "\n";

// Named blocks
// Automatically adjusting delimiters (#)
//
// #(?P<protocol>http[s]?)://(?P<url>.*)#
//
echo R::expression()
    ->
group(
       
R::group('protocol')
            ->
text('http')
            ->
char(R::chars('s')->optional())
    )
    ->
text('://')
    ->
group(
       
R::group('url')
            ->
char(R::anyChar()->zeroOrMore())
    ) .
"\n";

// Multiline expressions
//
// /^start\s+(^the)\s+show$/m
//
echo R::multiLineExpression()
    ->
startOfStringOrLine()
    ->
text('start')
    ->
whitespace()
    ->
group(
       
R::group()->startOfLine()->text('the')
    )
    ->
whitespace()
    ->
text('show')
    ->
endOfStringOrLine() . "\n";

// Look ahead, look behind
//
// /(?<=Lord )(Byron)/
//
echo R::expression()
    ->
lookBehind(
       
R::lookBehind()->text('Lord ')
    )
    ->
group(
       
R::group()->text('Byron')
    ) .
"\n";

// Include raw expressions
//
// #(?P<protocol>https?)://(?P<url>.*)#
//
echo R::expression()
    ->
group(
       
R::group('protocol')->raw('https?')
    )
    ->
text('://')
    ->
group(
       
R::group('url')->raw('.*')
    ) .
"\n";


Details

r - Regular Expression Builder (PHP)

r Is a PHP library to build regular expressions.

It is written for PHP 5 and it handles PCRE patterns in a fairly advanced level, but it does not cover the complete specification.

Why?

There are several reasons why one would want build a regular expression with code, rather than to write it down directly:

* For non-experts, the expression is easier to read * It is a handy tool to compose an expression at runtime, depending on different variables and rules * There is no need to escape characters, which is always a tricky part * No need to look up the syntax of little used constructs: the fluid interface allows for auto completion

Examples

These examples can also be found in the file 'examples.php'.

Full string match

/^Paradise Lost$/

R::expression()
	->startOfString()
	->text('Paradise Lost')
	->endOfString()

Alternate texts

/The (dog|cat)?basket fell off the roof/

R::expression()
	->text('The ')
	->group(
		R::group()->optional()->oneOfThese()->text('dog')->text('cat')
	)
	->text('basket fell off the roof')

Characters

/the [bc]?old man and the [^c]ee.*/

R::expression()
	->text('the ')
	->inChars(R::chars('bc')->optional())
	->text('old man and the ')
	->notInChars(R::chars('c'))
	->text('ee')
	->char(R::anyChar()->zeroOrMore())

Nested groups

/(<a href='([^']*)'>)/

R::expression()
	->group(
		R::group()
			->text("<a href='")
			->group(
				R::group()
					->notInChars(R::chars("'")->zeroOrMore())
			)
			->text("'>")
	)

Assertions

/\bkettle\b/

R::expression()
	->wordBoundary()
	->text('kettle')
	->wordBoundary()

Quantifiers

/[\d]{4}[a-z]{2}/

R::expression()
	->char(R::chars()->digit()->times(4))
	->char(R::chars()->letter()->times(2))

Named blocks, alternate delimiters (#)

#(?P<protocol>http[s]?)://(?P<url>.*)#

R::expression()
	->group(
		R::group('protocol')
			->text('http')
			->char(R::chars('s')->optional())
	)
	->text('://')
	->group(
		R::group('url')
			->char(R::anyChar()->zeroOrMore())
	)

Multiline expression

/^start\s+(^the)\s+show$/m

R::multiLineExpression()
	->startOfStringOrLine()
	->text('start')
	->whitespace()
	->group(
		R::group()->startOfLine()->text('the')
	)
	->whitespace()
	->text('show')
	->endOfStringOrLine()

Look behind, look ahead

/(?<=Lord )(Byron)/

R::expression()
    ->lookBehind(
        R::lookBehind()->text('Lord ')
    )
    ->group(
        R::group()->text('Byron')
    )

The old way is way easier!

Then write parts of the expression in the old style:

#(?P<protocol>https?)://(?P<url>.*)#

R::expression()
	->group(
		R::group('protocol')->raw('https?')
	)
	->text('://')
	->group(
		R::group('url')->raw('.*')
	)

Credits

The credits for the idea of creating a regular expression builder go to VerbalExpressions. It is a fascinating idea and I wanted to see how far it could go. I chose a different type of implementation based on nested fluid interface calls.


  Files folder image Files (23)  
File Role Description
Files folder imagecomponents (10 files)
Files folder imagetests (8 files)
Accessible without login Plain text file examples.php Example Example script
Accessible without login Plain text file LICENSE.md Lic. License text
Plain text file R.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file test.php Aux. Auxiliary script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:190
This week:0
All time:8,596
This week:673Up
User Comments (1)
Congratulations, this is a amazing class ;-) Regards
8 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar