Hi punit,

On Wed, 11 Dec 2013 21:04:39 +0530
punit jain <contactpunitj...@gmail.com> wrote:

> Hi,
> 
> I have a requirement where I need to capture phone number from different
> strings.
> 
> The strings could be :-
> 
> 
> 1. COMP TEL NO 919369721113  for computer science
> 
> 2. For Best Discount reach 092108493, from 6-9
> 
> 3. Your booking Confirmed, 9210833321
> 
> 4. price for free consultation call92504060
> 
> 5. price for free consultation call92504060number
> 
> I created a regex as below :-
> 
> #!/usr/bin/perl
> 
> my $line= shift @ARGV;
> 
> if($line =~
> /(?:(?:\D+|\s+)(?:(91\d{10}|0\d{10}|[7-9]\d{9}|0\d{11})|(?:(?:ph|cal)(\d+))))|(?:(?:(91\d{10}|0\d{10}|[7-9]\d{9}|0\d{11})|(?:(?:ph|cal)(\d+)))(?:\D+|\s+))/)
> {
> 
> print "one = $1";
> 
> 
> }
> It works fine for 1, 2,3 and prints number however for 4 and 5 one I get
> number in $2 rather than $1 tough I have pipe operator to check it.
> 
> Any clue how to fix this ?

I suggest you use named captures (a feature of perl-5.10.x-and-above) and then
you can do something like:

my $my_capture = ($+{'capture1'} // $+{'capture2'});

I think this is the best way to do it. (You can also do $1 // $2, but please
don't).

Regards,

        Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Why can’t we ever attempt to solve a problem in this country without having
a “War” on it? -- Rich Thomson, talk.politics.misc

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
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