Regular Expression to match unlimited number of options

Posted by Pekka on Stack Overflow See other posts from Stack Overflow or by Pekka
Published on 2010-03-28T20:37:26Z Indexed on 2010/03/28 20:43 UTC
Read the original article Hit count: 214

Filed under:
|
|

I want to be able to parse file paths like this one:

 /var/www/index.(htm|html|php|shtml)

into an ordered array:

 array("htm", "html", "php", "shtml")

and then produce a list of alternatives:

/var/www/index.htm
/var/www/index.html
/var/www/index.php
/var/www/index.shtml

Right now, I have a preg_match statement that can split two alternatives:

 preg_match_all ("/\(([^)]*)\|([^)]*)\)/", $path_resource, $matches);

Could somebody give me a pointer how to extend this to accept an unlimited number of alternatives (at least two)? Just regarding the regular expression, the rest I can deal with.

The rule is:

  • The list needs to start with a ( and close with a )

  • There must be one | in the list (i.e. at least two alternatives)

  • Any other occurrence(s) of ( or ) are to remain untouched.

© Stack Overflow or respective owner

Related posts about php

Related posts about regex