# string and pattern problem

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
1.) How to extract telephone number from string.
Form for telephone number is XXX/XXXX-XXX or XXX/XXX-XXX or XX/XXXX-XXX, so
I have problem with different number format.X is for numeric
3    /     4     - 3
or   3     / 3     -    3     or   2  /    4      -    3 number character
Eg. Phone numbers:
091/5444-333
098/255-555
042/255-555
01/6555-333

There is possible that string have more than one phone number.

2.) How to check position of pattern in some string
Eg.
\$pattern ="my";
\$string= "my dog is my pet";

Answer is: 0 , and 10

Perl programming it's very interesting, but some problem is too hard for me!

Thanks
Tajana

## Re: string and pattern problem

> Tajana (tajana@removeingko.com) wrote on MMMMCLXXXI September MCMXCIII in
> @@  1.) How to extract telephone number from string.
> @@  Form for telephone number is XXX/XXXX-XXX or XXX/XXX-XXX or XX/XXXX-XXX, so
> @@  I have problem with different number format.X is for numeric
> @@                                                          3    /     4     -
3
> @@  or   3     / 3     -    3     or   2  /    4      -    3 number character
> @@  Eg. Phone numbers:
> @@  091/5444-333
> @@  098/255-555
> @@  042/255-555
> @@  01/6555-333
>
>     m![0-9](?:[0-9]/[0-9]?|/[0-9])[0-9]-[0-9]!g;

If it doesn't have to be a single regex, other solutions are more flexible.
A hash that stores the allowable combinations can be directly read off
the specifications and is easily changed, if needed:

my %allowed;
\$allowed{ 3, 4, 3} = 1;
\$allowed{ 3, 3, 3} = 1;
\$allowed{ 2, 4, 3} = 1;

while ( m{ ((\d+) / (\d+) - (\d+)) }xg ) {
print "\$1\n" if \$allowed{ (), map length() => \$2, \$3, \$4};
}

This uses the syntax for "multidimensional array emulation" (see "\$;" in
perlvar).  It is still occasionally useful, though not essential to the
solution.

Anno

## Re: string and pattern problem

On 10 Feb 2005, Anno Siegel wrote:

> >     m![0-9](?:[0-9]/[0-9]?|/[0-9])[0-9]-[0-9]!g;
>
> If it doesn't have to be a single regex, other solutions are more flexible.
> A hash that stores the allowable combinations can be directly read off
> the specifications and is easily changed, if needed:
>
>     my %allowed;
>     \$allowed{ 3, 4, 3} = 1;
>     \$allowed{ 3, 3, 3} = 1;
>     \$allowed{ 2, 4, 3} = 1;
>
>     while ( m{ ((\d+) / (\d+) - (\d+)) }xg ) {
>         print "\$1\n" if \$allowed{ (), map length() => \$2, \$3, \$4};
>     }

Interesting.  I realize the expression to generate the hash key is in
list context -- I've just not seen it written that way before.  My
usual syntax would be

print "\$1\n" if \$allowed{ join \$; => map length() => \$2, \$3, \$4};

Thanks for the "golf lesson".

--
Regards,
Steven

## Re: string and pattern problem

> On 10 Feb 2005, Anno Siegel wrote:

> >     while ( m{ ((\d+) / (\d+) - (\d+)) }xg ) {
> >         print "\$1\n" if \$allowed{ (), map length() => \$2, \$3, \$4};
> >     }
>
>
>
>
> Interesting.  I realize the expression to generate the hash key is in
> list context -- I've just not seen it written that way before.  My
> usual syntax would be
>
>     print "\$1\n" if \$allowed{ join \$; => map length() => \$2, \$3, \$4};
>
> Thanks for the "golf lesson".

The content of the hash braces must look like a list, you need a literal
comma there.

However, looking at it again, the use of map is a bit silly here, it
would be better written

print "\$1\n" if \$allowed{ length \$2, length \$3, length \$4};

Now the commas come naturally.

Anno

## Re: string and pattern problem

> @@  Form for telephone number is XXX/XXXX-XXX or XXX/XXX-XXX or
XX/XXXX-XXX, so
> @@  I have problem with different number format.X is for numeric
> @@  3/4-3 or   3/3-3 or 2/4-3 number character
>
>     m![0-9](?:[0-9]/[0-9]?|/[0-9])[0-9]-[0-9]!g;

Thanks!
That is nice. Work, but I still don understand how that work.
I have some more possible format for phone number.
So new definition for phone number is something made by: [0-9], "/" and "-".
I don now length of phone number. (it possible that have more that one "/"
or "-")
Eg:
0047/367/5645-555
47/366/5555-5555-15
are also a phone number.

And please can you write some words about solutions, new and old also.

Thanks!

## Re: string and pattern problem

> @@  Form for telephone number is XXX/XXXX-XXX or XXX/XXX-XXX or
XX/XXXX-XXX, so
> @@  I have problem with different number format.X is for numeric
> @@  3/4-3 or   3/3-3 or 2/4-3 number character
>
>     m![0-9](?:[0-9]/[0-9]?|/[0-9])[0-9]-[0-9]!g;

Thanks!
That is nice. Work, but I still don understand how that work.
I have some more possible format for phone number.
So new definition for phone number is something made by: [0-9], "/" and "-".
I don now length of phone number. (it possible that have more that one "/"
or "-")
Eg:
0047/367/5645-555
47/366/5555-5555-15
are also a phone number.

And please can you write some words about solutions, new and old also.

Thanks!