Coder Social home page Coder Social logo

reversedregex's Introduction

reversedregex

Format a string so that it will match against a given PCRE

Example usage

require 'ReversedRegex.php';
$parser = new RegexParser('^/blog/(?<post_id>\d+)/.+$');
$reversed = new ReversedRegex($parser->regex()); // parse regex & reverse it
echo $reversed->format([
  'post_id' => 15,     // can be formatted by name or with index 0
  1 => 'My post title' // '.+' requires formatting too
]);
> /blog/15/My post title

Which tokens need formatting

  • Character classes: ., \w, \W, \d, \D, \s, \S, [abcdefg]
  • Variable-length repetitions of literals and non-literals: base+, base*, base?, base{2,3}, base{2,}. Note that if base is not a literal, you should provide a single formatting parameter to satisfy both base and the repetition.
  • Fixed-length repetitions of non-literals: base{4}. Same as above
  • Reversed lookarounds: (?!abc), (?<!abc)

Output verification

There's no verification at all. Consider the regex in the 'Example usage' section and the following code:

echo $reversed->format(['post_id' => 'lol', 1 => '']);
> /blog/lol/

This will not match against the regex. You should use something like preg_match on the output if you need verification.

What works as intended

Groups, named groups, character classes, lookarounds, anchors, string literals and repetitions

What is not implemented

  • Alternations (i.e. pipe operator): It is not possible to determine which side of the operation to format without further inspection of the involved argument counts, types and matching tokens.
$p = new RegexParser('\d+|[a-zA-Z]+');
$r = new ReversedRegex($p->regex());
$r->format(['hello']); // which one?
  • Backreferences: There is not a 1:1 relation between groups and formatting placeholders in some cases. More specifically, PCRE dictates that when a group is repeated the backreference matches the last captured value of the group. However, we format both the group and the repetition with a single value. The problem gets increasingly difficult when the group contains variable-length repetitions.
$p = new RegexParser('(\d)+\1');
$r = new ReversedRegex($p->regex());
$r->format(['123']); // (\d)+ formatted as '123', but how to tell what is '\1'?

reversedregex's People

Contributors

franciscoda avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar  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.