0

I'm curious if there is a way to generate the list of valid options from a regexp... something like this. Any ideas or hints welcome!

I want to do this in my PHP Code where I need to generate each possible outcome. You can see what I'm looking for here: http://uttool.com/text/regexstr/default.aspx

Expression: /(admin|agent)/Cutomers/View

Output Valid Options Array 0] '/admin/Customers/View' 1] '/agent/Customers/View'

Amb3rL4nn
  • 83
  • 10
  • Careful what you ask for. This could lead to an exponential output. – S.L. Barth is on codidact.com Feb 27 '15 at 14:05
  • The expressions are simple OR operations in most cases so the output will be limited. However I need to do this in my code so I'm not sure If I need to write something to parse it or if something already exist. – Amb3rL4nn Feb 27 '15 at 14:10
  • Please see http://stackoverflow.com/questions/776286/given-a-string-generate-a-regex-that-can-parse-similar-strings/776323#776323. There is a link to a free web-based "regex by example" generator. – Wiktor Stribiżew Feb 27 '15 at 14:10
  • I'm not looking on how to generate a regex. I'm looking to generate valid strings from a regex. If you see the site that outputs what I'm looking for. – Amb3rL4nn Feb 27 '15 at 14:18

2 Answers2

1

i've tried something like this:

function builder($string){
    $strend = explode('/',$string); 
    array_splice($strend,0,2);
    $strend=implode('/',$strend);
    preg_match('/\/(.*?)\//',$string,$m);
    $arr = explode('|',str_replace(array('(',')'),'',$m[1]));
    $resp = array();
    foreach($arr as $v){
        $resp[] = '/'.$v.'/'.$strend;
    }
    return $resp;
}
$str = '/(admin|agent|client)/Customers/View';
var_dump(builder($str));
miglio
  • 2,048
  • 10
  • 23
  • This works for what I need right now so I will accept it. Plus it can be expanded for slightly more complex expressions. From what I can tell there is not much for a library out there. – Amb3rL4nn Feb 27 '15 at 14:41
0

You can see a graphical representation here https://www.debuggex.com/

/(admin|agent)/Cutomers/View

Regular expression visualization

Debuggex Demo

If you have a dictionnary you can test against your regexp, without it is not possible juste imagine /.*/toto/tutu

Ôrel
  • 7,044
  • 3
  • 27
  • 46