7 pat t e r n m at c h I n g w I t h r e g u L a r e X p r e s s I o n s


Project: Phone Number and Email Address Extractor



Yüklə 397,03 Kb.
Pdf görüntüsü
səhifə19/25
tarix29.11.2022
ölçüsü397,03 Kb.
#71308
1   ...   15   16   17   18   19   20   21   22   ...   25
P A T T E R N M A T C H I N G W I T H

Project: Phone Number and Email Address Extractor
Say you have the boring task of finding every phone number and email 
address in a long web page or document. If you manually scroll through 
the page, you might end up searching for a long time. But if you had a pro-
gram that could search the text in your clipboard for phone numbers and 
email addresses, you could simply press 
ctrl
-A to select all the text, press 
ctrl
-C to copy it to the clipboard, and then run your program. It could 
replace the text on the clipboard with just the phone numbers and email 
addresses it finds.


180
Chapter 7
Whenever you’re tackling a new project, it can be tempting to dive right 
into writing code. But more often than not, it’s best to take a step back and 
consider the bigger picture. I recommend first drawing up a high-level plan 
for what your program needs to do. Don’t think about the actual code yet—
you can worry about that later. Right now, stick to broad strokes.
For example, your phone and email address extractor will need to do 
the following:
1. Get the text off the clipboard.
2. Find all phone numbers and email addresses in the text.
3. Paste them onto the clipboard.
Now you can start thinking about how this might work in code. The 
code will need to do the following:
1. Use the 
pyperclip
module to copy and paste strings.
2. Create two regexes, one for matching phone numbers and the other 
for matching email addresses.
3. Find all matches, not just the first match, of both regexes.
4. Neatly format the matched strings into a single string to paste.
5. Display some kind of message if no matches were found in the text.
This list is like a road map for the project. As you write the code, you 
can focus on each of these steps separately. Each step is fairly manageable 
and expressed in terms of things you already know how to do in Python.

Yüklə 397,03 Kb.

Dostları ilə paylaş:
1   ...   15   16   17   18   19   20   21   22   ...   25




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin