Click here to get back home

Perl pattern matching and extraction

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl pattern matching and extraction Deepan - M.Sc(SE) - 03MW06 03-08-2008
Posted by Deepan - M.Sc(SE) - 03MW06 on March 8, 2008, 8:48 am
Please log in for more thread options
Hi,
I would like to thank everyone for their help to my previous
post. Actually i didn't explained my need completely. Here's it

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 extract "sony" from it. In the
sense i should be able to extract whatever that is available in the
last between "/" and "/". Please help me to solve this.

Thanks,
Deepan

Posted by Gunnar Hjalmarsson on March 8, 2008, 9:21 am
Please log in for more thread options
[ You shouldn't have started a new thread for the same problem! ]

Deepan - M.Sc(SE) - 03MW06 wrote:
> Hi,
> I would like to thank everyone for their help to my previous
> post. Actually i didn't explained my need completely. Here's it
>
> 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 extract "sony" from it. In the
> sense i should be able to extract whatever that is available in the
> last between "/" and "/". Please help me to solve this.

This can still be easiest solved with split().

( split /\//, $url )[-1]

Otherwise you can do:

if ( $url =~ m#.*/(.+)/# ) {
print "$1\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by dummy on March 12, 2008, 8:08 am
Please log in for more thread options
On Sat, 8 Mar 2008 05:48:25 -0800 (PST), "Deepan - M.Sc(SE) - 03MW06"

>Hi,
> I would like to thank everyone for their help to my previous
>post. Actually i didn't explained my need completely. Here's it
>
> 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 extract "sony" from it. In the
>sense i should be able to extract whatever that is available in the
>last between "/" and "/". Please help me to solve this.
>
>Thanks,
>Deepan

This seems to be what you're trying to get:

use strict; use warnings;
my $s = " junk /fee/fie/fum/sony/Help! I'm stuck in a regex!/ foo";
(my @result) = $s =~ m!/([^/]+)(?=/)!g;
map {print "[$_]\n"}@result;

The output is:

[fee]
[fie]
[fum]
[sony]
[Help! I'm stuck in a regex!]

Similar ThreadsPosted
Pattern Matching and Extraction January 22, 2007, 2:51 pm
Perl pattern extraction March 8, 2008, 7:09 am
Pattern extraction March 10, 2008, 4:14 am
Pattern extraction March 10, 2008, 4:42 am
Help! Complex Pattern Extraction with Key/Value Pairs and Reg Exp? November 17, 2004, 6:10 pm
Help with Pattern matching. Matching multiple lines from while reading from a file. May 2, 2007, 11:37 pm
perl pattern matching August 31, 2004, 8:57 am
perl pattern matching March 12, 2005, 8:25 am
Pattern matching in perl August 10, 2006, 9:25 am
RE Perl Pattern matching April 2, 2008, 1:23 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap