164
Chapter 7
On each iteration of the
for
loop, a new chunk of 12 characters from
message
is assigned to the variable
chunk
. For example, on the first itera-
tion,
i
is
0
, and
chunk
is assigned
message[0:12]
(that is, the string
'Call me
at 4'
). On the next iteration,
i
is
1
, and
chunk
is assigned
message[1:13]
(the string
'all me at 41'
). In other words, on each iteration of the
for
loop,
chunk
takes on the following values:
•
'Call me at 4'
•
'all me at 41'
•
'll me at 415'
•
'l me at 415-'
• . . . and so on.
You pass
chunk
to
isPhoneNumber()
to see whether it matches the phone
number pattern , and if so, you print the chunk.
Continue to loop through
message
, and eventually the 12 characters
in
chunk
will be a phone number. The loop goes through the entire string,
testing each 12-character piece and printing any
chunk
it finds that satisfies
isPhoneNumber()
. Once we’re done going through
message
, we print
Done
.
While the string in
message
is short in this example, it could be millions
of characters long and the program would still run in less than a second. A
similar program that finds phone numbers using regular expressions would
also run in less than a second, but regular expressions make it quicker to
write these programs.
Dostları ilə paylaş: