string and pattern problem

T

Tad McClellan

Tajana said:
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
m#\d{3}/\d{4}-\d{3}#g

or 3 / 3 - 3
m#\d{3}/\d{3}-\d{3}#g

or 2 / 4 - 3 number character
m#\d{2}/\d{4}-\d{3}#g


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


That isn't much of a pattern. It looks like a constant string.

$string= "my dog is my pet";

Answer is: 0 , and 10


perldoc -f index


Or if you really meant for $pattern to be a pattern:

perldoc -f pos
 
A

Anno Siegel

Abigail said:
Tajana ([email protected]) wrote on MMMMCLXXXI September MCMXCIII in
<URL:mad:@ 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]{2}(?:[0-9]/[0-9]?|/[0-9])[0-9]{3}-[0-9]{3}!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
 
S

Steven Kuo

m![0-9]{2}(?:[0-9]/[0-9]?|/[0-9])[0-9]{3}-[0-9]{3}!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".
 
T

Tajana

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!
Please help me!

Thanks
Tajana
 
A

Anno Siegel

Steven Kuo said:
On 10 Feb 2005, Anno Siegel wrote:




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
 
T

Tad McClellan

Tajana said:
I have some more possible format for phone number.
So new definition for phone number is


And after we fix that, you'll come back with a 3rd added thing?

Then a 4th?

Then a 5th?


You will use up all of your coupons quickly by changing the
specification _after_ the implementation has been completed.

Think about your problem more before you ask others to
volunteer their time helping to solve it.

Learn as much as you can about regular expressions:

perldoc perlrequick
perldoc perlretut
perldoc perlre

then ask a coherant question about the particular aspect that
you are having trouble understanding.
 
T

Tajana

@@ 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]{2}(?:[0-9]/[0-9]?|/[0-9])[0-9]{3}-[0-9]{3}!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!
 
T

Tajana

@@ 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]{2}(?:[0-9]/[0-9]?|/[0-9])[0-9]{3}-[0-9]{3}!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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,168
Messages
2,570,914
Members
47,455
Latest member
Delilah Code

Latest Threads

Top