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'

4
  • Careful what you ask for. This could lead to an exponential output. Commented Feb 27, 2015 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. Commented Feb 27, 2015 at 14:10
  • Please see stackoverflow.com/questions/776286/…. There is a link to a free web-based "regex by example" generator. Commented Feb 27, 2015 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. Commented Feb 27, 2015 at 14:18

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

2 Comments

any idea of a php library or function that would let me do this? I need to do this in my code.
Do what ? Have a dictionary of all url to test against your regexp ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.