Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Question on Regex

by Anonymous Monk
on Nov 18, 2012 at 13:41 UTC ( [id://1004410]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have a small question, Pls can you answer meI am not a perl programmer

I have a string(as a general one)  $string = ".co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Tradewind/?APath=2.21.0.0.0". Here I need to extract the "Tradewind". How can i extract the text at the same position(as there is not only Tradewind & there are more). I need a general regex solution which can extract the text of the same position of Tradewind.

Thank you!

Replies are listed 'Best First'.
Re: Question on Regex
by Athanasius (Archbishop) on Nov 18, 2012 at 14:20 UTC

    You can also do this without a regex, by using split on the ‘/’ character to produce a list, and then subscripting the list to get the desired field:

    #! perl use strict; use warnings; my $count = 0; for my $line (<DATA>) { my $name = (split '/', $line)[4]; print "Name #", ++$count, " is '", $name, "'\n"; } __DATA__ .co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Tradewind/?APath=2.21.0 +.0.0 .com/Stuff/Somewhere/ABCD789/Peabody/?APath=2.0.12.1.3 .com.au/More-Stuff/Anywhere/XYZ12345/Perkins/?APath=4.5.6.7.8

    Output:

    0:14 >perl 390_SoPW.pl Name #1 is 'Tradewind' Name #2 is 'Peabody' Name #3 is 'Perkins' 0:18 >

    Hope that helps,

    Athanasius <°(((><contra mundum

      Thanks Athanasius. I got what I was looking. Thanks a lot. :)

        FYI:

        #!/usr/bin/perl use strict; use warnings; use Benchmark qw ( :hireswallclock cmpthese timethese ); our $string = qq (.co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Trade +wind/?APath=2.21.0.0.0); sub karlgoethebier { our $string; $string =~ m/.+\/.+\/.+\/.+\/(.+)\/.+/; return $1; } sub athanasius { our $string; return (split '/', $string)[4]; } my $results = timethese (-10, { 'karlgoethebier' => 'karlgoethebier', 'athanasius' => 'athanasius', }); cmpthese($results); __END__ Karls-Mac-mini:Desktop karl$ ./tradewind.pl Benchmark: running athanasius, karlgoethebier for at least 10 CPU seco +nds... athanasius: 10.4769 wallclock secs (10.47 usr + 0.00 sys = 10.47 CPU) + @ 627362.46/s (n=6568485) karlgoethebier: 10.4287 wallclock secs (10.42 usr + 0.00 sys = 10.42 +CPU) @ 105188.77/s (n=1096067) Rate karlgoethebier athanasius karlgoethebier 105189/s -- -83% athanasius 627362/s 496% --

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Question on Regex
by karlgoethebier (Abbot) on Nov 18, 2012 at 14:00 UTC

    Hello, perhaps this helps.

    #!/usr/bin/perl use strict; use warnings; my $string = qq (.co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Tradew +ind/?APath=2.21.0.0.0); $string =~ m/.+\/.+\/.+\/(.+)\/.+/; print $1;

    Update: I did not count right, sorry...

    $string =~m/.+\/.+\/.+\/.+\/(.+)\/.+/;

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Thanks Karl, Got it... :)

        Yes, i know that i tend to sink before i type. But i'm working hard to get better ;-) Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Question on Regex
by Anonymous Monk on Nov 18, 2012 at 13:50 UTC
    Can you explain what position?

      Here the Tradewind comes on 5th position after the 1st "/". Either I need to extract the text using the position of the "/" or by any other means. Thanks Monk!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1004410]
Approved by Athanasius
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-19 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found