Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl pattern extraction

Reply
Thread Tools

Perl pattern extraction

 
 
Deepan - M.Sc(SE) - 03MW06
Guest
Posts: n/a
 
      03-08-2008
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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-08-2008

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



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-08-2008
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-08-2008
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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-09-2008
Chris Mattern <(E-Mail Removed)> wrote:
> On 2008-03-08, Deepan - M.Sc(SE) - 03MW06 <(E-Mail Removed)> 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?
>>

> 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";
}
---------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl pattern matching and extraction Deepan - M.Sc(SE) - 03MW06 Perl Misc 1 03-08-2008 02:21 PM
Pattern Matching and Extraction doni Perl Misc 10 01-23-2007 11:37 PM
Extraction of attribute values from pattern matches Peekachu Java 1 07-10-2006 07:59 PM
Help! Complex Pattern Extraction with Key/Value Pairs and Reg Exp? aekalman Perl Misc 6 11-22-2004 10:59 PM
Pattern Search and Extraction of Floating Point number to csv file Ram Laxman Perl 1 07-15-2004 04:16 PM



Advertisments