Skip to content

Commit

Permalink
* Add Alternation class
Browse files Browse the repository at this point in the history
* Refactor Range class
* Fix docblocks
* Update README

Update README.md

upadte
  • Loading branch information
niklongstone committed Jun 16, 2015
1 parent 6c29e2e commit d1c4441
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 49 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

0.4.0 / 2015-06-16
=================
* Add alternation
* Add not in range feature
* Fix minor bugs

0.3.0 / 2015-05-06
=================
* Add debug feature
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,58 @@
[![Quality Score](https://img.shields.io/scrutinizer/g/niklongstone/regex-reverse.svg?style=flat-square)](https://scrutinizer-ci.com/g/niklongstone/regex-reverse)


Regular expression reverter, given a regular expression will output a string that will match it
Regular expression reverter, generates a string from the given regular expression.

## Install

Via Composer
Via [Composer](https://getcomposer.org/download/)

``` bash
$ composer require niklongstone/regex-reverse
$ composer require niklongstone/regex-reverse:'^0.4.0'
```

## Usage

``` php
<?php
require ('regex-reverse/vendor/autoload.php');
use RegRev\RegRev;

echo RegRev::generate('\d'); //ouput a random number
```
For a list of useful regular expression, please visit: [Awesome PCRE](https://github.com/niklongstone/awesome-regular-expression)

## Supported expressions

#### Character classes

| Expression | Description | Result |
|------------|-------------|-------------------------|
| \d | digit | a number |
| \D | non digit | an alpha char |
| \w | word | a alphanumeric char |
| \W | word | a non alphanumeric char |
| \W | non word | a non alphanumeric char |
| \s | space | a blank space |
| \S | space | a non blank space |
| \S | non space | a non blank space |

## Conditional and subgroup
#### Conditional and subgroup
| Expression | Description | Example | Result |
|---------------|------------------|-----------|-------------|
| () | subgroup | (\d\w)+@ | 97a987Ss@ |
| (a|b) | alternation | (a|i)nt | int |
| * | zero or more | \d* | 123502 |
| + | one or more | \d+ | 32133 |
| ? | zero or one | \d? | 3 |
|{n} {n,} {n,m} |from n to m times | \w{1,3} | np |

## Ranges
#### Ranges
| Expression | Description | Result |
|------------------|------------------|--------------|
| [0-9] | range 0 to 9 | 7 |
| [a-d] | range a to b | b |
| [0-9c-f] | range 0-9 or c-f | d |
| [ab5\.] | chars in list | b |
| [^ab5\.] | chars not in list| 8 |

##Examples

Expand All @@ -69,6 +76,7 @@ echo RegRev::generate('\d'); //ouput a random number
|`SE[1-9]{1}\d{1}\s[A-Z]{2}\d{2}`| SE27 GU35 | london SE post code |
| `organi[sz]e` | organise or organize | US or UK spelling |


## Other features
- debug: `RegRev::debug()` will return an array of messages

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "niklongstone/regex-reverse",
"description": "Regular Expression reverter, generates a string that match the provided regular expression",
"description": "Regular Expression reverter, generates a string from a provided regular expression",
"keywords": [
"regular expression",
"reverse", "test", "data", "regex", "match"
"regular expression", "generation", "test", "data", "regex", "preg", "PCRE"
],
"homepage": "https://github.com/niklongstone/reverse-regex",
"type": "library",
Expand Down
35 changes: 33 additions & 2 deletions src/RegRev/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function __construct()
array(
'name' => 'Range',
'type' => 'Range\Range',
'chars' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY./\\()"\':,.;<>~!@#$%^&*|+=[]{}`~?-',
'pattern' => array('/^\[[^\]]*\]/')
),
array(
Expand All @@ -73,12 +74,32 @@ public function __construct()
'type' => 'Quantifier\NTimes',
'pattern' => array('/^\{(\d*),?(\d*)?\}/')
),
array(
'name' => 'Alternation',
'type' => 'Range\Alternation',
'pattern' => array('|')
),
array(
'name' => 'Blank space',
'type' => 'CharType\Generic',
'pattern' => array('\h','\s'),
'returnValue' => ' '
),
/**
* Ignored
*/
array(
'name' => 'Start',
'type' => 'CharType\Generic',
'pattern' => array('^'),
'returnValue' => ''
),
array(
'name' => 'End',
'type' => 'CharType\Generic',
'pattern' => array('$'),
'returnValue' => ''
),
/**
* Escaped meta-character
*/
Expand Down Expand Up @@ -154,6 +175,12 @@ public function __construct()
'pattern' => array('\+'),
'returnValue' => '+'
),
array(
'name' => 'Escaped minus',
'type' => 'CharType\Generic',
'pattern' => array('\-'),
'returnValue' => '-'
),
array(
'name' => 'Escaped left curly bracket',
'type' => 'CharType\Generic',
Expand Down Expand Up @@ -186,15 +213,19 @@ public function getConfig()
/**
* Sets the configuration.
*
* @param $config
* @param array $config
*/
public function setConfig(array $config)
public function setConfig($config)
{
$this->config = $config;
}

/**
* Setup the configuration.
*
* @param array $parameters
*
* @return ExpressionContainer
*/
public function setUp($parameters)
{
Expand Down
8 changes: 6 additions & 2 deletions src/RegRev/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ class Debug
private static $messages = array();

/**
* @param $message
* Sets the debug messages.
*
* @param string $message
*/
static public function setMessage($message)
{
self::$messages[] = $message;
}

/**
* Gets the debug messages.
*
* @return array
*/
static public function getMessages()
Expand All @@ -35,7 +39,7 @@ static public function getMessages()
}

/**
* @return array
* Clear the debug messages.
*/
static public function clear()
{
Expand Down
47 changes: 41 additions & 6 deletions src/RegRev/ExpressionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,76 @@
namespace RegRev;

/**
* Class ExpressionContainer
* Class ExpressionContainer,
* handles the available regular expressions.
*/
class ExpressionContainer implements \Iterator
{
private $position = 0;

private $expressions = array();

/**
* Constructor.
*/
public function __construct()
{
$this->position = 0;
}

/**
* Sets the expression.
*
* @param mixed $expression
*/
public function set($expression)
{
$this->expressions[] = $expression;
}

public function rewind() {
/**
* Rewinds the container index.
*/
public function rewind()
{
$this->position = 0;
}

public function current() {
/**
* Gets the current element.
*
* @return mixed
*/
public function current()
{
return $this->expressions[$this->position];
}

public function key() {
/**
* Returns the current key.
*
* @return int
*/
public function key()
{
return $this->position;
}

public function next() {
/**
* Increases the current index.
*/
public function next()
{
++$this->position;
}

public function valid() {
/**
* Checks if the expression is set.
*
* @return bool
*/
public function valid()
{
return isset($this->expressions[$this->position]);
}
}
2 changes: 1 addition & 1 deletion src/RegRev/Metacharacter/CharType/CharListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace RegRev\Metacharacter\CharType;

/**
* Defines an interface for a list of characters.
* Defines an interface to handle list of characters.
*/
interface CharListInterface
{
Expand Down
14 changes: 11 additions & 3 deletions src/RegRev/Metacharacter/CharacterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public function getName()
/**
* Sets a name for debug the class
*
* @param string
*
* @return string
* @param string $name
*/
public function setName($name)
{
Expand Down Expand Up @@ -104,11 +102,15 @@ public function setMatch($match)
}

/**
* Handles the generation process.
*
* @return string
*/
abstract public function generate();

/**
* Sets chain successor.
*
* @param CharacterHandler $handler
*/
final public function setSuccessor(CharacterHandler $handler)
Expand All @@ -118,6 +120,8 @@ final public function setSuccessor(CharacterHandler $handler)
}

/**
* Set previous handler.
*
* @param CharacterHandler $handler
*/
final public function setPrevious(CharacterHandler $handler)
Expand All @@ -126,6 +130,8 @@ final public function setPrevious(CharacterHandler $handler)
}

/**
* Gets previous handler.
*
* @return CharacterHandler
*/
final public function getPrevious()
Expand All @@ -134,6 +140,8 @@ final public function getPrevious()
}

/**
* Gets the result.
*
* @param string $result
*
* @return string
Expand Down
3 changes: 2 additions & 1 deletion src/RegRev/Metacharacter/GroupType/Subpattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

/**
* Class Subpattern,
* handles the subpattern match.
* handles the sub-pattern match.
*/
class Subpattern extends CharacterHandler
{
const SQUARE_BRACKETS_PATTERN = '/(^\(.*\))/';

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RegRev/Metacharacter/Quantifier/OneOrMore.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function getQuanitity($min, $max)
protected function generateQuanitity($quantity)
{
$result = null;
for($i = 0; $i < $quantity; $i ++) {
for ($i = 0; $i < $quantity; $i ++) {
if ($this->getPrevious()) {
get_class($this->getPrevious());
$result .= $this->getPrevious()->generate();
Expand Down
2 changes: 1 addition & 1 deletion src/RegRev/Metacharacter/Quantifier/ZeroOrOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class ZeroOrOne extends CharacterHandler
*/
public function generate()
{
return null;
return;
}
}
Loading

0 comments on commit d1c4441

Please sign in to comment.