| Recommend this page to a friend! | 
|  Download | 
| Info | Example |  Files |  Install with Composer |  Download | Reputation | Support forum | Blog | Links | 
| Ratings | Unique User Downloads | Download Rankings | ||||
| Not enough user ratings | Total: 190 | All time:  8,596 This week: 673  | ||||
| Version | License | PHP version | Categories | |||
| regexp-r 1.0.0 | MIT/X Consortium ... | 5 | PHP 5, Text processing | 
| Description | Author | |
| This class can generate regular expression strings from rules. | 
| 
<?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.
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
These examples can also be found in the file 'examples.php'.
/^Paradise Lost$/
R::expression()
	->startOfString()
	->text('Paradise Lost')
	->endOfString()
/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')
/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())
/(<a href='([^']*)'>)/
R::expression()
	->group(
		R::group()
			->text("<a href='")
			->group(
				R::group()
					->notInChars(R::chars("'")->zeroOrMore())
			)
			->text("'>")
	)
/\bkettle\b/
R::expression()
	->wordBoundary()
	->text('kettle')
	->wordBoundary()
/[\d]{4}[a-z]{2}/
R::expression()
	->char(R::chars()->digit()->times(4))
	->char(R::chars()->letter()->times(2))
#(?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())
	)
/^start\s+(^the)\s+show$/m
R::multiLineExpression()
	->startOfStringOrLine()
	->text('start')
	->whitespace()
	->group(
		R::group()->startOfLine()->text('the')
	)
	->whitespace()
	->text('show')
	->endOfStringOrLine()
/(?<=Lord )(Byron)/
R::expression()
    ->lookBehind(
        R::lookBehind()->text('Lord ')
    )
    ->group(
        R::group()->text('Byron')
    )
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('.*')
	)
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 (23) | 
| File | Role | Description | ||
|---|---|---|---|---|
|  components (10 files) | ||||
|  tests (8 files) | ||||
|    examples.php | Example | Example script | ||
|    LICENSE.md | Lic. | License text | ||
|  R.php | Class | Class source | ||
|    README.md | Doc. | Documentation | ||
|    test.php | Aux. | Auxiliary script | ||
|  Files (23) | / | components | 
| File | Role | Description | 
|---|---|---|
|  RAnyChar.php | Class | Class source | 
|  RCharBase.php | Class | Class source | 
|  RChars.php | Class | Class source | 
|  RExpression.php | Class | Class source | 
|  RGroup.php | Class | Class source | 
|  RLookAhead.php | Class | Class source | 
|  RLookBehind.php | Class | Class source | 
|  RMultiLineExpression.php | Class | Class source | 
|  RQuantifiable.php | Class | Class source | 
|  RSingleLineExpression.php | Class | Class source | 
|  Files (23) | / | tests | 
| File | Role | Description | 
|---|---|---|
|    characters.php | Example | Example script | 
|    escaping.php | Example | Example script | 
|    expressions.php | Example | Example script | 
|    groups.php | Example | Example script | 
|    lookbehind.php | Example | Example script | 
|    modifiers.php | Example | Example script | 
|    quantifiers.php | Example | Example script | 
|    special.php | Example | Example 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 | 
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% | 
 | 
 | 
| User Comments (1) | |||||
| 
 | |||||
| Applications that use this package | 
 If you know an application of this package, send a message to the author to add a link here.
 If you know an application of this package, send a message to the author to add a link here.