$var = 'about/';
$var =~ s/^(.*)\/?$/index.php?action=$1/;
I want it to match and substitute the trailing slash. But it isn’t, I’m getting this result:
index.php?action=about/
What am I doing wrong? I’ve tried wrapping it in parenthesis (\/)? and including the question mark in the parenthesis (\/?). Not including the preceding forward slash obviously doesn’t do me any good. So how can I get it to eat the slash when it’s there?
Your problem is, that the
.*is greedy as well. Try using.*?.The Regex engine is first expanding
.*as far as possible, then checks if the regex can match the input. In your case, it does, since the trailing slash is optional. It then goes home satisfied.