![]() ![]() The rest of the function checks that the string follows the pattern of a phone number: the number must have the first hyphen after the area code ➌, three more numeric characters ➍, then another hyphen ➎, and finally four more numbers ➏. Then it checks that the area code (that is, the first three characters in text) consists of only numeric characters ➋. First the code checks that the string is exactly 12 characters ➊. If any of these checks fail, the function returns False. The isPhoneNumber() function has code that does several checks to see whether the string in text is a valid phone number. When this program is run, the output looks like this: Open a new file editor tab and enter the following code then save the file as isPhoneNumber.py: ![]() Let’s use a function named isPhoneNumber() to check whether a string matches this pattern, returning either True or False. You know the pattern if you’re American: three numbers, a hyphen, three numbers, a hyphen, and four numbers. Say you want to find an American phone number in a string. Finding Patterns of Text Without Regular Expressions Finally, at the end of the chapter, you’ll write a program that can automatically extract phone numbers and email addresses from a block of text. ![]() I’ll show you basic matching with regular expressions and then move on to some more powerful features, such as string substitution and creating your own character classes. In this chapter, you’ll start by writing a program to find text patterns without using regular expressions and then see how to use regular expressions to make the code much less bloated. When you’re a nerd, you forget that the problems you solve with a couple keystrokes can take other people days of tedious, error-prone work to slog through. Knowing can mean the difference between solving a problem in 3 steps and solving it in 3,000 steps. In fact, tech writer Cory Doctorow argues that we should be teaching regular expressions even before programming: Regular expressions are huge time-savers, not just for software users but also for programmers. Regular expressions are helpful, but few non-programmers know about them even though most modern text editors and word processors, such as Microsoft Word or OpenOffice, have find and find-and-replace features that can search based on regular expressions. We also recognize all sorts of other text patterns every day: email addresses have symbols in the middle, US social security numbers have nine digits and two hyphens, website URLs often have periods and forward slashes, news headlines use title case, social media hashtags begin with # and contain no spaces, and more. This is how you, as a human, know a phone number when you see it: 41 is a phone number, but 4,155,551,234 is not. You may not know a business’s exact phone number, but if you live in the United States or Canada, you know it will be three digits, followed by a hyphen, and then four more digits (and optionally, a three-digit area code at the start). Regular expressions go one step further: they allow you to specify a pattern of text to search for. ![]() You may be familiar with searching for text by pressing CTRL-F and entering the words you’re looking for. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |