GithubHelp home page GithubHelp logo

dangoodman / reverseregex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pointybeard-forks/reverseregex

0.0 0.0 0.0 161 KB

Use Regular Expressions to generate text string in PHP.

License: Other

PHP 100.00%

reverseregex's Introduction

ReverseRegex

Build Status

Use Regular Expressions to generate strings.

Installation

This library is installed via Composer. To install, use composer require pointybeard/reverse-regex in your application.

Usage

<?php

declare(strict_types=1);

use ReverseRegex\Lexer;
use ReverseRegex\Random\SimpleRandom;
use ReverseRegex\Parser;
use ReverseRegex\Generator\Scope;

require "vendor/autoload.php";

$pattern = "[a-z0-9]{10}"; // 10 random letters and numbers

$lexer = new Lexer($pattern);
$random = new SimpleRandom();
$parser = new Parser($lexer, new Scope(), new Scope());
$generator = $parser->parse()->getResult();

$result = '';

$parser = new Parser($lexer,new Scope(),new Scope());

var_dump($generator->generate($result, $random));
// string(10) "j2ydisgoks"

See https://github.com/pointybeard-forks/ReverseRegex/tree/master/examples for more examples.

Notes When Writing Regular Expressions

  1. Escape all meta-characters i.e. if you need to escape the character in a regex you will need to escape here.
  2. Not all meta-characters are suppported see list below.
  3. Use \X{####} to specify unicode value use [\X{####}-\X{####}] to specify range.
  4. Unicdoe \p not supported, I could not find a port of UCD to php, maybe in the future support be added.
  5. Quantifiers are applied to left most group, literal or character class.
  6. Beware of the + and * quantifers they apply a possible maxium number of occurances up to PHP_INT_MAX.

Supported Syntax

Example Description Resulting String
(abcf) Support literals this would generate string 'abcf'
\((abcf)\) Escape meta characters as you normally would in a regex '(abcf)'
[a-z] Character Classes are supported 'a'
a{5} Quantifiers supported always last group or literal or character class 'aaaaa'
a{1,5} Range Quantifiers supported 'aa'
a|b|c Alternation supported pick one of three at random 'b'
a|(y|d){5} Groups supported with alternation and quantifiers 'ddddd', 'a', or 'yyyyy'
\d Digit shorthand equ [0-9] '1'
\w word character shorthand equ [a-zA-Z0-9_] 'j'
\W Non word character shorthand equ [^a-zA-Z0-9_] 'j'
\s White space shorthand ASCII only ' '
\S Non White space shorthand ASCII only 'i'
. Dot all ASCII characters '$'
* + ? Short hand quantifiers, recommend not use them
\X{00FF}[\X{00FF}-\X{00FF}] Unicode ranges
\xFF[\xFF-\xFF] Hex ranges

About

Requirements

  • This library works with PHP 7.2 or above.

Support

If you believe you have found a bug, please report it using the GitHub issue tracker.

Author

License

"ReverseRegex" is released under the MIT License. See LICENCE for details.

reverseregex's People

Contributors

icomefromthenet avatar mhlavac avatar pointybeard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.