I want to be able to parse file paths like this:
/var/www/index.(htm|html|php|shtml)
into an ordered array:
array("htm", "html", "php", "shtml")
and then create a list of alternatives:
/var/www/index.htm /var/www/index.html /var/www/index.php /var/www/index.shtml
Now I have a preg_match statement that can separate the two alternatives:
preg_match_all ("/\(([^)]*)\|([^)]*)\)/", $path_resource, $matches);
Can someone give me a pointer on how to extend this to accept an unlimited number of alternatives (at least two)? Just regarding regex, the rest I can deal with.
Rule:
The list should begin with ( and close with )
The list must have one | (i.e. at least two alternatives)
Any other event ( or ) should remain intact.
Update: I also need to deal with several pair brackets, such as:
/var/(www|www2)/index.(htm|html|php|shtml)
Sorry, I did not say this right away.
Update 2: If you want to do what I am trying to do on the file system, note that glob () already displays this functionality out of the box. There is no need to implement custom solutiom. See @Gordon below for more details.
php regex preg-match
Pekka ์
source share