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

Regex Doubt

by Anonymous Monk
on Nov 12, 2012 at 08:26 UTC ( #1003379=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 doubt regarding how to extract the locations from the data. Pls hav a look at the below code,

$data = "Part Time - Weekend Receptionist - Barton le Clay - Barton-le +-Clay (near Luton)"; $data = "Part-Time Weekend Day Veterinary Nurse - Milton Keynes"; $data = "Receptionist - Barton-le-Clay (near Luton)"; $data = "Full Time - Veterinary Nurse - Glasgow Referral - Glasgow";

The above are the various types. The below is the code I am currently using.!!!

$location = $1 if ($data =~ /-(.*)/i); $location = $1 if ($location =~ /-(.*)/i);

Now I am using the above weird technique to extract this. But its noe reliable. Any Monks with a Helping hand. Many thanks in advance.

Comment on Regex Doubt
Select or Download Code
Re: Regex Doubt
by space_monk (Chaplain) on Nov 12, 2012 at 08:36 UTC
    The only reliable way to detect the location appears to be to lock the regex to the end of the string and then do a non-greedy match from the last " - " (dash with spaces)
    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Regex Doubt
by Athanasius (Monsignor) on Nov 12, 2012 at 09:01 UTC

    Perhaps you’re looking for something along these lines?

    #! perl use strict; use warnings; use Data::Dump; my @records; while (my $line = <DATA>) { chomp(my @r = split / - /, $line); push @records, [ @r ]; } dd @records; __DATA__ Part Time - Weekend Receptionist - Barton le Clay - Barton-le-Clay (ne +ar Luton) Part-Time Weekend Day Veterinary Nurse - Milton Keynes Receptionist - Barton-le-Clay (near Luton) Full Time - Veterinary Nurse - Glasgow Referral - Glasgow

    Output:

    18:58 >perl 381_SoPW.pl ( [ "Part Time", "Weekend Receptionist", "Barton le Clay", "Barton-le-Clay (near Luton)", ], ["Part-Time Weekend Day Veterinary Nurse", "Milton Keynes"], ["Receptionist", "Barton-le-Clay (near Luton)"], ["Full Time", "Veterinary Nurse", "Glasgow Referral", "Glasgow"], ) 18:59 >

    Hope that helps,

    Athanasius <°(((><contra mundum

      Thanks Athanasius. :)

Re: Regex Doubt
by pvaldes (Chaplain) on Nov 12, 2012 at 11:30 UTC

    I love to cheat (and I love to check too). Look at the last line of data

    /i is said yet, so its use here is intentionally omitted

    use strict; use warnings; while (<DATA>){ if (/([Bb]arton-[Ll]e-[Cc]lay|[Mm]ilton\s+[Kk]eynes|[Gg]lasgow|[Bb]art +on-le-[cC]lay \(near [Ll]utton\))\s?$/){ print $1,"\n"} else {print "-- what? --\n"} } __DATA__ Part Time - Weekend Receptionist - Barton le Clay - Barton-le-Clay (ne +ar Luton) Part-Time Weekend Day Veterinary Nurse - Milton keynes Receptionist - barton-le-clay (near luton) Full time liar - Glasgow (Australia)
Re: Regex Doubt
by 2teez (Priest) on Nov 12, 2012 at 12:39 UTC

    Hi,
    Playing around with regex, I came up with this. This is not absolute but it solves this problem, and some others.
    Assuming that the locations you want to extract are the names of places after job titles.

    use warnings; use strict; while (<DATA>) { chomp; my $str = reverse $_; if ( $str =~ m/((.*?)(\s\-\s)?(.*?))\3/ ) { my $matched_str = scalar reverse $1; $matched_str =~ s/^(-\s?)?//; print $matched_str, $/; } } __DATA__ Part Time - Weekend Receptionist - Barton le Clay - Barton-le-Clay (ne +ar Luton) Part-Time Weekend Day Veterinary Nurse - Milton Keynes Receptionist - Barton-le-Clay (near Luton) Full Time - Veterinary Nurse - Glasgow Referral - Glasgow Full Time - Professional House wife - Umuezebi Family House - New Have +n-Upper-Chime-Enugu ( near 9th Mile ) Full time liar - Glasgow (Australia)
    Output:
    Barton le Clay - Barton-le-Clay (near Luton)
    Milton Keynes
    Barton-le-Clay (near Luton)
    Glasgow Referral - Glasgow
    Umuezebi Family House - New Haven-Upper-Chime-Enugu ( near 9th Mile )
    Glasgow (Australia)

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-27 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls