PHP Classes

PHP Regex Analyzer and Composer: Analyze and compose regular expressions

Recommend this page to a friend!
  Info   View files Documentation   View files View files (18)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2022-08-08 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 132 All time: 9,220 This week: 204Up
Version License PHP version Categories
regexanalyzer 1.1.0Artistic License5PHP 5, Text processing, Parsers
Description Author

This package be used to analyze and compose regular expressions.

It can take a regular expression string and parse it to return the structure of the parts that define the expression.

The package can also compose a new regular expression programmatically by adding the different parts that compose the expression.

The package also provides versions of the same code in Python and in JavaScript.

Innovation Award
PHP Programming Innovation award winner
October 2020
Winner
Regular expressions are often used to check if a string matches a given format and extract values from parts of that string.

This package provides a solution to compose and parse a regular expression to make it easier to edit the regular expression and change it to match expression formats that are of the interest of the developer.

Manuel Lemos
Picture of Nikos M.
Name: Nikos M. is available for providing paid consulting. Contact Nikos M. .
Classes: 17 packages by
Country: Greece Greece
Age: 46
All time rank: 9119 in Greece Greece
Week rank: 20 Up1 in Greece Greece Up
Innovation award
Innovation award
Nominee: 7x

Winner: 2x

Details

Regex.Analyzer and Regex.Composer

A generic, simple & intuitive Regular Expression Analyzer & Composer for PHP, Python, Node.js / Browser / XPCOM Javascript

Regex v.1.1.0

see also:

  • ModelView a simple, fast, powerful and flexible MVVM framework for JavaScript
  • tico a tiny, super-simple MVC framework for PHP
  • LoginManager a simple, barebones agnostic login manager for PHP, JavaScript, Python
  • SimpleCaptcha a simple, image-based, mathematical captcha with increasing levels of difficulty for PHP, JavaScript, Python
  • Dromeo a flexible, and powerful agnostic router for PHP, JavaScript, Python
  • PublishSubscribe a simple and flexible publish-subscribe pattern implementation for PHP, JavaScript, Python
  • Importer simple class & dependency manager and loader for PHP, JavaScript, Python
  • Contemplate a fast and versatile isomorphic template engine for PHP, JavaScript, Python
  • HtmlWidget html widgets, made as simple as possible, both client and server, both desktop and mobile, can be used as (template) plugins and/or standalone for PHP, JavaScript, Python (can be used as plugins for Contemplate)
  • Paginator simple and flexible pagination controls generator for PHP, JavaScript, Python
  • Formal a simple and versatile (Form) Data validation framework based on Rules for PHP, JavaScript, Python
  • Dialect a cross-vendor & cross-platform SQL Query Builder, based on GrammarTemplate, for PHP, JavaScript, Python
  • DialectORM an Object-Relational-Mapper (ORM) and Object-Document-Mapper (ODM), based on Dialect, for PHP, JavaScript, Python
  • Unicache a simple and flexible agnostic caching framework, supporting various platforms, for PHP, JavaScript, Python
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python

These were used mostly as parts of other projects but uploaded here as standalone.

See /test/js/test.js under /test folder for examples of how to use

Regex.Analyzer Live Playground Example:

Live Playground Example

Regex.Composer Live Playground Example:

Live Playground Example

Example: (see /test/js/test.js)

//
// use as: node test.js "your_regex_here" > output.txt
"use strict";

var echo = console.log;


var Regex = require('../../src/js/Regex.js');

echo("Regex.VERSION = " + Regex.VERSION);

echo("Testing Regex.Composer");
echo("===============================================================");

var identifierSubRegex = Regex.Composer( )
                
                .characterGroup( )
                    .characters( '_' )
                    .range( 'a', 'z' )
                .end( )
                
                .characterGroup( )
                    .characters( '_' )
                    .range( 'a', 'z' )
                    .range( '0', '9' )
                .end( ).zeroOrMore( )
            
                .partial( );

var outregex = Regex.Composer( )
                    
                .SOL( )
                
                .nonCaptureGroup( ).either( )
                    .regexp( identifierSubRegex )
                    .namedGroup( 'token' ).literal( 'aabb' ).end( )
                    .any( )
                    .space( )
                    .digit( false ).oneOrMore( )
                .end( 2 ).zeroOrMore( false )
                
                .backReference( 'token' )
                
                .EOL( )
                
                .compose( 'i' );
    
echo("Partial        : " + identifierSubRegex);
echo("Composed       : " + outregex.pattern.toString());
echo("Expected       : " + "/^(?:[_a-z][_a-z0-9]|(\\\\aabb\\\\)|.|\\s|\\D+)?\\1$/i");
echo("Output         : " + JSON.stringify(outregex, null, 4));
echo("===============================================================");
echo();

var anal, peekChars, sampleStr, minLen, maxLen, groups, regexp,
    inregex = "/(?P<named_group>[abcde]+)fgh(?P=named_group)(?# a comment)/i";

echo("Testing Regex.Analyzer");
echo("===============================================================");

// test it
anal = Regex.Analyzer( inregex );
peekChars = anal.peek( );
minLen = anal.minimum( );
maxLen = anal.maximum( );
regexp = anal.compile( {i:anal.fl.i?1:0} );
sampleStr = anal.sample( 1, 5 );
groups = anal.groups();
for(var i=0; i<5; i++)
{
    var m = sampleStr[i].match(regexp);
    sampleStr[i] = {sample:sampleStr[i], match:(m ? 'yes' : 'no'), groups: {}};
    if ( m )
    {
        for(var g in groups)
            if ( Object.prototype.hasOwnProperty.call(groups,g) )
                sampleStr[i].groups[g] = m[groups[g]];
    }
}

echo("Input                                       : " + inregex.toString( ));
echo("Regular Expression                          : " + anal.input());
echo("Regular Expression Flags                    : " + Object.keys(anal.fl).join(','));
echo("Reconstructed Regular Expression            : " + anal.source());
echo("===============================================================");
echo("Regular Expression Syntax Tree              : ");
echo(JSON.stringify(anal.tree(true), null, 4));
echo("===============================================================");
echo("Regular Expression (Named) Matched Groups   : ");
echo(JSON.stringify(groups, null, 4));
echo("===============================================================");
echo("Regular Expression Peek Characters          : ");
echo(JSON.stringify({positive:Object.keys(peekChars.positive||{}),negative:Object.keys(peekChars.negative||{})}, null, 4));
echo("===============================================================");
echo("Regular Expression Minimum / Maximum Length : ");
echo(JSON.stringify({minimum:minLen,maximum:-1===maxLen?'unlimited':maxLen}, null, 4));
echo("===============================================================");
echo("Regular Expression Sample Match Strings     : ");
echo(JSON.stringify(sampleStr, null, 4));
echo("===============================================================");

Result

Regex.VERSION = 1.0.0
Testing Regex.Composer
===============================================================
Partial        : [_a-z][_a-z0-9]*
Composed       : /^(?:[_a-z][_a-z0-9]|(\\aabb\\)|.|\s|\D+)?\1$/i
Expected       : /^(?:[_a-z][_a-z0-9]|(\\aabb\\)|.|\s|\D+)?\1$/i
Output         : {
    "source": "^(?:[_a-z][_a-z0-9]|(\\\\aabb\\\\)|.|\\s|\\D+)?\\1$",
    "flags": "i",
    "groups": {
        "1": 1,
        "token": 1
    },
    "pattern": {}
}
===============================================================

Testing Regex.Analyzer
===============================================================
Input                                       : /(?P<named_group>[abcde]+)fgh(?P=named_group)(?# a comment)/i
Regular Expression                          : (?P<named_group>[abcde]+)fgh(?P=named_group)(?# a comment)
Regular Expression Flags                    : i
Reconstructed Regular Expression            : ([abcde]+)fgh\1
===============================================================
Regular Expression Syntax Tree              : 
{
    "type": "Sequence",
    "value": [
        {
            "type": "Group",
            "value": {
                "type": "Sequence",
                "value": [
                    {
                        "type": "Quantifier",
                        "value": {
                            "type": "CharacterGroup",
                            "value": [
                                {
                                    "type": "Characters",
                                    "value": [
                                        "a",
                                        "b",
                                        "c",
                                        "d",
                                        "e"
                                    ]
                                }
                            ]
                        },
                        "flags": {
                            "MatchOneOrMore": 1,
                            "min": 1,
                            "max": -1,
                            "isGreedy": 1
                        }
                    }
                ]
            },
            "flags": {
                "NamedGroup": 1,
                "GroupName": "named_group",
                "GroupIndex": 1
            }
        },
        {
            "type": "String",
            "value": "fgh"
        },
        {
            "type": "Special",
            "value": "1",
            "flags": {
                "BackReference": 1,
                "GroupName": "named_group",
                "GroupIndex": 1
            }
        },
        {
            "type": "Comment",
            "value": " a comment"
        }
    ]
}
===============================================================
Regular Expression (Named) Matched Groups   : 
{
    "1": 1,
    "named_group": 1
}
===============================================================
Regular Expression Peek Characters          : 
{
    "positive": [
        "a",
        "b",
        "c",
        "d",
        "e",
        "A",
        "B",
        "C",
        "D",
        "E"
    ],
    "negative": []
}
===============================================================
Regular Expression Minimum / Maximum Length : 
{
    "minimum": 5,
    "maximum": "unlimited"
}
===============================================================
Regular Expression Sample Match Strings     : 
[
    {
        "sample": "AdbFGHAdb",
        "match": "yes",
        "groups": {
            "1": "Adb",
            "named_group": "Adb"
        }
    },
    {
        "sample": "CDfghCD",
        "match": "yes",
        "groups": {
            "1": "CD",
            "named_group": "CD"
        }
    },
    {
        "sample": "CBCfghCBC",
        "match": "yes",
        "groups": {
            "1": "CBC",
            "named_group": "CBC"
        }
    },
    {
        "sample": "BbaAFGHBbaA",
        "match": "yes",
        "groups": {
            "1": "BbaA",
            "named_group": "BbaA"
        }
    },
    {
        "sample": "EfghE",
        "match": "yes",
        "groups": {
            "1": "E",
            "named_group": "E"
        }
    }
]
===============================================================
  Files folder image Files  
File Role Description
Files folder imagesrc (6 directories)
Files folder imagetest (2 files, 3 directories)
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file UNLICENSE Lic. License text

  Files folder image Files  /  src  
File Role Description
Files folder imageactionscript (1 file)
Files folder imagec (1 file)
Files folder imagejava (1 file)
Files folder imagejs (2 files)
Files folder imagephp (1 file)
Files folder imagepython (2 files)

  Files folder image Files  /  src  /  actionscript  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  c  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  java  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  js  
File Role Description
  Accessible without login Plain text file Regex.js Data Auxiliary data
  Accessible without login Plain text file Regex.min.js Data Auxiliary data

  Files folder image Files  /  src  /  php  
File Role Description
  Plain text file Regex.php Class Class source

  Files folder image Files  /  src  /  python  
File Role Description
  Accessible without login Plain text file Regex.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  test  
File Role Description
Files folder imagejs (2 files)
Files folder imagephp (2 files)
Files folder imagepython (2 files)
  Accessible without login Image file screenshot.png Data Auxiliary data
  Accessible without login Image file screenshot2.png Data Auxiliary data

  Files folder image Files  /  test  /  js  
File Role Description
  Accessible without login Plain text file output.txt Doc. Documentation
  Accessible without login Plain text file test.js Data Auxiliary data

  Files folder image Files  /  test  /  php  
File Role Description
  Accessible without login Plain text file output.txt Doc. Documentation
  Accessible without login Plain text file test.php Example Example script

  Files folder image Files  /  test  /  python  
File Role Description
  Accessible without login Plain text file output.txt Doc. Documentation
  Accessible without login Plain text file test.py Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:132
This week:0
All time:9,220
This week:204Up
For more information send a message to info at phpclasses dot org.