Zanurkuj w Pythonie/Analiza przypadku: Przetwarzanie numerów telefonów: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Linia 81:
>>>
 
# Jedyna zmiana jakiej dokonaliśmy od ostatniego kroku to zamiana wszystkich + na *. Zamiast \D+ pomiędzy częściami numeru telefonu dopasowujemy teraz \D*. Pamiętasz, że + oznacza "1 lub więcej"? * oznacza "0 lub więcej". Tak więc teraz jesteśmy w stanie przetworzyć numer nawet bez separatorów.
# The only change you've made since that last step is changing all the + to *. Instead of \D+ between the parts of the phone number, you now match on \D*. Remember that + means “1 or more”? Well, * means “zero or more”. So now you should be able to parse phone numbers even when there is no separator character at all.
# Nareszcie działa! Dlaczego? Dopasowany został początek łańcucha, grupa 3 cyfr (800), potem zero znaków nienumerycznych, potem znowu zapamiętywana grupa 3 cyfr (555), znowu zero znaków nienumerycznych, zapamiętywana grupa 4 cyfr (1212), zero znaków nienumerycznych, numer wewnętrzny (1234) i nareszcie koniec łańcucha.
# Lo and behold, it actually works. Why? You matched the beginning of the string, then a remembered group of three digits (800), then zero non-numeric characters, then a remembered group of three digits (555), then zero non-numeric characters, then a remembered group of four digits (1212), then zero non-numeric characters, then a remembered group of an arbitrary number of digits (1234), then the end of the string.
# Inne odmiany też działają np. numer rozdzielony kropkami ze spacją i x przed numerem wewnętrznym.
# Other variations work now too: dots instead of hyphens, and both a space and an x before the extension.
# Wreszcie udało się też rozwiązać problem z brakiem numeru wewnętrznego. Tak czy siak groups() zwraca nam krotke z 4 elementami, ale ostatni jest tutaj pusty.
# Finally, you've solved the other long-standing problem: extensions are optional again. If no extension is found, the groups() method still returns a tuple of four elements, but the fourth element is just an empty string.
# Nie cierpię przekazywać złych wieści, ale jeszcze nie skończyliśmy. Co tutaj jest nie tak? Przed numerem kierunkowym znajduje się dodatkowy znak - (, a nasze wyrażenie zakłada, że numer kierunkowy znajduje się na samym przodzie. Nie ma problemu, możemy zastosować tą samą metodę co do znaków rozdzielających.
# I hate to be the bearer of bad news, but you're not finished yet. What's the problem here? There's an extra character before the area code, but the regular expression assumes that the area code is the first thing at the beginning of the string. No problem, you can use the same technique of “zero or more non-numeric characters” to skip over the leading characters before the area code.
 
Następny przykład pokazuje jak sobie radzić ze znakami wiodącymi w numerach telefonów.
The next example shows how to handle leading characters in phone numbers.
 
'''ExamplePrzykład 7.14. Obsługa znaków na początku numeru telefonu'''
 
>>> phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') #(1)