tags:

views:

58

answers:

1

Hi,

i have a regular expression to remove certain parts from a URI. However it doesn't take into account multiple parts in a way that works :-). Can somebody assist?

$regex = '~/{(.*?)}\*~'

$uri = '/user/{action}/{id}*/{subAction}*';
$newuri = preg_replace($regex, ''  , $uri); 

//$newuri = /user/
//Should be: $newuri = /user/{action}/

I know it matches the following part as one match:

/{action}/{id}/{subAction}

But it should match the following two seperately:

/{id}*

/{subAction}*

+6  A: 

To me it looks like your {(.*?)}\* test is matching all of {action}/{id}*, which judging from what you've written isn't what you want.

So change the Kleene closure to be less greedy:

'~/{([^}]*)}\*~'

But do you really need to capture the part inside the curly braces? Seems to me you could go with this one instead:

'~/{[^}]*}\*~'

Either way, the [^}]* part guarantees that the expression will not match {action}/ because it doesn't end in an asterisk.

Welbog
Thanks for thinking along! This is exactly what i need!
Ropstah