|
Posted by Deepan - M.Sc(SE) - 03MW06 on March 8, 2008, 7:09 am
Please log in for more thread options
Hi,
my $url = "/pages-cell.net/deepan/sony/";
if($url =~ m/\/(.*)\//g)
{
my @result = $1;
return @result;
}
What i need is that i should be able to get anything that is between /
and /. Here i should be able to get pages-cell.net,deepan,sony into
@result but something is wrong somewhere. Please help me to solve
this?
Thanks,
Deepan
|
|
Posted by Ben Morrow on March 8, 2008, 7:29 am
Please log in for more thread options
> Hi,
>
> my $url = "/pages-cell.net/deepan/sony/";
>
> if($url =~ m/\/(.*)\//g)
> {
> my @result = $1;
> return @result;
> }
>
> What i need is that i should be able to get anything that is between /
> and /. Here i should be able to get pages-cell.net,deepan,sony into
> @result but something is wrong somewhere. Please help me to solve
> this?
* is greedy by default, meaning it matches as much as possible. So you
either make it not greedy
m!/(.*?)/!g
or you are more specific about what can match
m!/([^/]*)/!g
. Note that m//g in an 'if' clause will only return the first match,
making the /g somewhat pointless.
If you were thinking that
my @result = $1;
would assign *all* the matches to @result, then you have misunderstood
how scalar variables work in Perl. $1 can only ever contain one value.
If you want to split $url on slashes, you can just use split:
my @result = split '/', $url;
shift @result;
Ben
>
> Thanks,
> Deepan
|
|
Posted by Gunnar Hjalmarsson on March 8, 2008, 7:34 am
Please log in for more thread options Deepan - M.Sc(SE) - 03MW06 wrote:
>
> my $url = "/pages-cell.net/deepan/sony/";
>
> if($url =~ m/\/(.*)\//g)
> {
> my @result = $1;
> return @result;
> }
>
> What i need is that i should be able to get anything that is between /
> and /. Here i should be able to get pages-cell.net,deepan,sony into
> @result but something is wrong somewhere. Please help me to solve
> this?
my @result = $url =~ m#/(.*?)(?=/)#g;
Better yet:
my @result = split /\//, $url;
perldoc -f split
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
Posted by John W. Krahn on March 8, 2008, 1:01 pm
Please log in for more thread options Deepan - M.Sc(SE) - 03MW06 wrote:
> Hi,
>
> my $url = "/pages-cell.net/deepan/sony/";
>
> if($url =~ m/\/(.*)\//g)
> {
> my @result = $1;
> return @result;
> }
>
> What i need is that i should be able to get anything that is between /
> and /. Here i should be able to get pages-cell.net,deepan,sony into
> @result but something is wrong somewhere. Please help me to solve
> this?
my @result = $url =~ /[^\/]+/g
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
Posted by Chris Mattern on March 8, 2008, 3:38 pm
Please log in for more thread options > Hi,
>
> my $url = "/pages-cell.net/deepan/sony/";
>
> if($url =~ m/\/(.*)\//g)
> {
> my @result = $1;
> return @result;
> }
>
> What i need is that i should be able to get anything that is between /
> and /. Here i should be able to get pages-cell.net,deepan,sony into
> @result but something is wrong somewhere. Please help me to solve
> this?
>
No. m\/(.*)\//g only returns one string; m *always* only returns one
string. In this case, the pattern will match one /, as many characters
as possible, including / (because * is greedy), and then another /.
Therefore, $1 will be "pages-cell.net/deepan/sony". Why in the world
would you expect to be able to get an array (@result) from a scalar ($1)?
perldoc -f split should tell you about what you need to be using.
--
Christopher Mattern
NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
|
| Similar Threads | Posted | | Perl pattern matching and extraction | March 8, 2008, 8:48 am |
| Pattern extraction | March 10, 2008, 4:14 am |
| Pattern extraction | March 10, 2008, 4:42 am |
| Pattern Matching and Extraction | January 22, 2007, 2:51 pm |
| Help! Complex Pattern Extraction with Key/Value Pairs and Reg Exp? | November 17, 2004, 6:10 pm |
| Column extraction in perl | September 15, 2006, 12:31 pm |
| hiw do i perform this extraction | August 21, 2006, 4:12 am |
| FTP Link Extraction | January 20, 2007, 10:06 am |
| Statistics Extraction | January 23, 2007, 3:25 pm |
| Help: Content extraction | May 9, 2008, 10:30 pm |
|