On Thu, Jul 9, 2009 at 15:18, Umar Draz<i_deb...@yahoo.com> wrote:
> Dear User!
>
> I want to get all telephone and mobile number from a string and save into a 
> variable. Here is my example what i am doing.
>
> #!/usr/bin/perl
>
> $str = "This is my string my mobile number is 0300-4459899, 042-8494949 
> 041-8580880 now the string is complete"
>  while($line =~ /(([0-9]{3,4}[-]?)?[0-9]{7})/g){
>    $tel =  $1
> }
>
> Now the above code is working file for me all telephone numbers and mobile 
> number are saved in $tel.
>
> No my question is how to handle spaces e.g
>
> $str
> = "This is my string my mobile number is 042-6715171 0 30 0- 4 4 5 9 8
> 9 9,  0 42-84 94 94 9, 0 4 1 - 85 80  8 8 0 now the string is complete"
> while($line =~ /(([0-9]{3,4}[-]?)?[0-9]{7})/g){
>
>    $tel =  $1
>
> }
>
>
> now this time only (042-6715171) are saved in $tel variable.
>
> Would you please help me to solve this issue.
>
> Regards,
>
> Umar
>
>
>

Remove all of the spaces, tabs, and other whitespace characters before
searching for numbers:

#!/usr/bin/perl

use strict;
use warnings;

#!/usr/bin/perl

my $str = "This is my string my mobile number is 042-6715171 0 30 0- 4
4 5 9 8 9 9,  0 42-84 94 94 9, 0 4 1 - 85 80  8 8 0 now the string is
complete";

(my $tmp = $str) =~ s/\s+//g;
while ($tmp =~ /(([0-9]{3,4}[-]?)?[0-9]{7})/g) {
           my $tel = $1;
           print "$tel\n";
}


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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