PHP Regex Builder: Generate regular expression strings from rules

Recommend this page to a friend!
  Info   View files Example   View files View files (23)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 174 This week: 1All time: 8,639 This week: 289Up
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  
Name: Patrick Van Bergen <contact>
Classes: 2 packages by
Country: The Netherlands The Netherlands
Innovation award
Innovation award
Nominee: 1x

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  
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

  Files folder image Files  /  components  
File Role Description
  Plain text file RAnyChar.php Class Class source
  Plain text file RCharBase.php Class Class source
  Plain text file RChars.php Class Class source
  Plain text file RExpression.php Class Class source
  Plain text file RGroup.php Class Class source
  Plain text file RLookAhead.php Class Class source
  Plain text file RLookBehind.php Class Class source
  Plain text file RMultiLineExpression.php Class Class source
  Plain text file RQuantifiable.php Class Class source
  Plain text file RSingleLineExpression.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file characters.php Example Example script
  Accessible without login Plain text file escaping.php Example Example script
  Accessible without login Plain text file expressions.php Example Example script
  Accessible without login Plain text file groups.php Example Example script
  Accessible without login Plain text file lookbehind.php Example Example script
  Accessible without login Plain text file modifiers.php Example Example script
  Accessible without login Plain text file quantifiers.php Example Example script
  Accessible without login Plain text file special.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:174
This week:1
All time:8,639
This week:289Up
User Comments (1)
Congratulations, this is a amazing class ;-) Regards
4 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar
 

For more information send a message to info at phpclasses dot org.