Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Regex Help!

by Anonymous Monk
on Sep 19, 2013 at 07:32 UTC ( [id://1054791]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks

I have a doubt regarding a Perl regex, pls check the code below,

my $loc_data = "Location: England Location: Scotland Location: Finland "; my $location = $1 if ($loc_data =~ /Location:(.*)/i); print $location ; exit;

Here, I am getting the output as England, because its the first one. Okay, now what modifications we should do if we want to take the second one(Here, its Scotland) or the third one(Here, its Finland). Is there a way to do this?

Thanks in advance.

Replies are listed 'Best First'.
Re: Regex Help!
by kcott (Archbishop) on Sep 19, 2013 at 07:54 UTC

    Take a look at perlre: Modifiers:

    • There's no case-insensitivity: you don't need the 'i' modifier.
    • $loc_data is a multiline string: you'll need the 'm' modifier.
    • You want repeat matches: that'll be the 'g' modifier.
    • Depending on how you rewrite your regex, you may also want the 's' modifier.
    • Also, to improve the legibility of your regex, see the 'x' modifier.

    -- Ken

      Thanks. I tried this, got the second one, but still unable to get the 3rd one!..

      my $loc = $1 if ($loc_data =~ /.+Location:(.*)/);

      what if there is more than 3 Location: in the $loc_data? Is there a way we can define it by the number? means we define 1 and we get the first? if its 2 we get 2nd, like that!

        Where do you use the g and m modifiers?
Re: Regex Help!
by daxim (Curate) on Sep 19, 2013 at 09:22 UTC
    You need to learn yourself how stuff works. You cannot bother other people all the time which you can trivially look up yourself in your learning materials or the official documentation.

    my (@locations) = "Location: England Location: Scotland Location: Finland " =~ /Location: (\p{General_Category +=Letter}+)/gms; print $locations[0]; # England print $locations[1]; # Scotland print $locations[2]; # Finland
    update: made \pL more explicit
Re: Regex Help! (perlrequick Extracting matches)
by Anonymous Monk on Sep 19, 2013 at 07:36 UTC
      Thanks, tried, but still unable to do it. I got the second but unable to get the 3rd!

        Thanks, tried, but still unable to do it.

        Tried what? What did you copy/paste?

        Maybe see also More matching

Re: Regex Help!
by Laurent_R (Canon) on Sep 19, 2013 at 09:30 UTC

    A small example under the Perl debugger, storing the countries into a @locations array:

    DB<16> p $loc_data Location: England Location: Scotland Location: Finland DB<17> @locations = $loc_data =~ /Location: ([^\n]+)/g; DB<18> x @locations 0 'England' 1 'Scotland' 2 'Finland'

    The regex used assumes that the record separator is '\n', it might need to be adapted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1054791]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found