Perl pattern extraction

  • Thread starter Deepan - M.Sc(SE) - 03MW06
  • Start date
D

Deepan - M.Sc(SE) - 03MW06

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
 
B

Ben Morrow

Quoth "Deepan - M.Sc(SE) - 03MW06 said:
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
 
G

Gunnar Hjalmarsson

Deepan said:
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
 
J

John W. Krahn

Deepan said:
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
 
T

Tad J McClellan

Chris Mattern said:
No. m\/(.*)\//g only returns one string; m *always* only returns one
string.


No it doesn't.

The m// operator always returns one string when in scalar context.

(in fact, *every* operator can return only one thing in scalar context.)

m// in list context can potentially evaluate to more than one string.

---------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'foo bar';

my @matches = m/(\w+)\s+(\w+)/;

foreach (@matches) {
print "$_\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top