Greetings,

Many thanks to Lawrence Statton, Shlomi Fish and Nathan Hilterbrand for your 
detailed explanation.

Here is the complete working code after your suggestions

Courtesy : Lawrence Statton and Shlomi Fish


[code]
use strict;

use warnings;
use utf8; #As suggested by Lawrence Statton
binmode STDOUT,":utf8"; #As suggested by Lawrence Statton

while (<DATA>) {
    #if block as suggested by Shlomi Fish
    if(my($name, $phone, $address) = /^([a-zA-Z ]+):([\-\d]+):([\w, ]+)$/) {
        #^([a-zA-Z ]+):([\-\d]+):([\w, ]+)$
        #Modified the regular expression to match - (dash) as suggested by 
Lawrence Statton
        print "Name : $name\n" if defined $name;
        print "Phone : $phone\n" if defined $phone;
        print "Address : $address\n" if defined $address; 
    } else {
        print "Did not match the regex\n";
    }
    
}

__DATA__
Sachin Tendulkar:408-724-0140:23, Brooke Street, Sunnyvale,CA 94088
Mahendra Singh Dhoni:408-456-1234:76, Charles Street, Washington, MA 02131
Rahul Dravid:408-253-3122:123, Queens Street, Canberra, Australia
Virendar Sehwag:293-259-5395:15 Hilton Avenue, Sydney, Australia
[/code]

[output]
Name : Sachin Tendulkar
Phone : 408-724-0140
Address : 23, Brooke Street, Sunnyvale,CA 94088
Name : Mahendra Singh Dhoni
Phone : 408-456-1234
Address : 76, Charles Street, Washington, MA 02131
Name : Rahul Dravid
Phone : 408-253-3122
Address : 123, Queens Street, Canberra, Australia
Name : Virendar Sehwag
Phone : 293-259-5395
Address : 15 Hilton Avenue, Sydney, Australia
[/output]


best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
 From: Lawrence Statton <lawre...@cluon.com>
To: beginners@perl.org 
Sent: Wednesday, 14 August 2013 10:17 PM
Subject: Re: Help on regex
 

On 08/14/2013 11:26 AM, *Shaji Kalidasan* wrote:
>     my ($name, $phone, $address) = /^([a-zA-Z ]+):([\-\d]+):([\w, ])$/;

And here is your second problem ...

the "name" part matches.

The phone-number part matches if you either fix the numbers in the
source data, or allow — in the regexp

THe address part will only match if the remainder of the string after
the second colon is exactly ONE character in the set [\w, ]

Adding a single plus to make /^([a-zA-Z ]+):([\-\d]+):([\w, ]+)$/
will work if you fix the input data.

Adding a — to your regexp will work with the existing data

adding
use utf8;
binmode STDOUT,":utf8";

will stop sqawks about wide-character printing

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to