Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl Regex Help!

by Anonymous Monk
on Mar 23, 2013 at 11:59 UTC ( [id://1025026]=perlquestion: print w/replies, xml ) Need Help??

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

Dear PerlMonks,

I have a simple question. kindly help me if possible....

I have some names. some have a country code in front of that name, but some don't. The country code is either 2 digit or 3 digit codes and is seperated with a hyphen. I wish to extract the country code using perl regex (remember some doesn't have the country codes). Below are some of the example names.

$name = "UK-Storee Leaader"; $name = "US-Inventory Specialiist"; $name = "Verifiication Engiineer - Techniical"; $name = "CHN-Speciialist"; $name = "Software Engiineer - Telecom"; $name = "ESP-Busiiness Manager"; $name = "Software Engiineer - Productiiviity"; $name = "FRA-Busiiness Speciialist"; $name = "CAN-Busiiness Speciialist"; $name = "CHE-Inventory Specialiist"; $name = "HK-Iinventory Speciialiist"; $name = "ITA-Speciialist"; $name = "NLD-Busiiness Specialiist";

Pls note that some are not having country codes but having a hyphen. I Only want to extract if there is a country code. Basically if the digit count matches 2 or 3 before the first hyphen, I need to do the extract and leave the rest(if not like that).

Pls can anyone suggest me a simple regex for this. I am planning to do the $name one at a time, so dont need to loop or something... just that basic regex which separate those county codes.

Thank you Monks.

Replies are listed 'Best First'.
Re: Perl Regex Help!
by choroba (Cardinal) on Mar 23, 2013 at 12:04 UTC
    If the country code is always all uppercase, you can use the following simple regex:
    #!/usr/bin/perl use warnings; use strict; for ('UK-Storee Leaader', 'US-Inventory Specialiist', 'Verifiication Engiineer - Techniical', 'CHN-Speciialist', 'Software Engiineer - Telecom', 'ESP-Busiiness Manager', 'Software Engiineer - Productiiviity', 'FRA-Busiiness Speciialist', 'CAN-Busiiness Speciialist', 'CHE-Inventory Specialiist', 'HK-Iinventory Speciialiist', 'ITA-Speciialist', 'NLD-Busiiness Specialiist', ) { if (/^([[:upper:]]+)-/) { print "Country code: $1 in $_.\n"; } else { print "No country code in $_.\n"; } }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank You for your time & help...
Re: Perl Regex Help!
by 2teez (Vicar) on Mar 23, 2013 at 12:19 UTC

    You can do this, using the OP data:

    use warnings; use strict; while(<DATA>){ chomp; if(/(\w{2,3})-/){ print $1,$/; } } __DATA__ $name = "UK-Storee Leaader"; $name = "US-Inventory Specialiist"; $name = "Verifiication Engiineer - Techniical"; $name = "CHN-Speciialist"; $name = "Software Engiineer - Telecom"; $name = "ESP-Busiiness Manager"; $name = "Software Engiineer - Productiiviity"; $name = "FRA-Busiiness Speciialist"; $name = "CAN-Busiiness Speciialist"; $name = "CHE-Inventory Specialiist"; $name = "HK-Iinventory Speciialiist"; $name = "ITA-Speciialist"; $name = "NLD-Busiiness Specialiist";

    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
      Thanks for your time and help...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-26 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found