Click to See Complete Forum and Search --> : Match without including in substitution


st33d
03-24-2007, 12:18 PM
I have this regex:

#! /usr/bin/perl -w
#/date/2007-02-22/ or /date/2007-02-22
#/index.php?date=2007-02-22 without a trailing slash
$s1 = "/date/2007-02-22/";
$s1 =~ s/date\/(.*)\/?/index.php?date=$1/;
print $s1;

But I can't get it to match the date folder with the optional slash at the end without including it in the replacement.

I want: /index.php?date=2007-02-22
not: /index.php?date=2007-02-22/

Is there a way of matching a string but not including it in the substitution?

Jeff Mott
03-24-2007, 02:03 PM
$s1 =~ s/date\/(.*?)\/?/index.php?date=$1/;

st33d
03-24-2007, 04:05 PM
Thanks that works fine.

But could someone tell why it works? I don't get how the "?" at the end of the parenthesis affects the slash outside it.

Jeff Mott
03-25-2007, 02:09 AM
The .* matches any character any number of times. By default, it does so greedily, meaning that it will match as many characters as it can. In your case, it was matching every character including the slash at the end. And since matching another trailing slash after that is optional, it matches successfully. Adding the question mark (.*?) changes the matching mode to non-greedy. So instead of matching as many as it can, it will match as few as it can.