@@ -82,7 +82,7 @@ time.sleep(SECONDS_IN_A_DAY)
8282** Bad:**
8383``` python
8484address= ' One Infinite Loop, Cupertino 95014'
85- city_zip_code_regex= ' ^[^,\\ ]+[,\\ \s]+(.+?)\s*(\d{5} )?$'
85+ city_zip_code_regex= r ' ^ [^ , \\ ]+ [, \\ \s ]+ ( . +? ) \s * ( \d {5} ) ? $ '
8686matches= re.match(city_zip_code_regex, address)
8787
8888save_city_zip_code(matches[1 ], matches[2 ])
@@ -94,23 +94,22 @@ It's better, but we are still heavily dependent on regex.
9494
9595``` python
9696address= ' One Infinite Loop, Cupertino 95014'
97- city_zip_code_regex= ' ^[^,\\ ]+[,\\ \s]+(.+?)\s*(\d{5} )?$'
97+ city_zip_code_regex= r ' ^ [^ , \\ ]+ [, \\ \s ]+ ( . +? ) \s * ( \d {5} ) ? $ '
9898matches= re.match(city_zip_code_regex, address)
9999
100100city, zip_code= matches.groups()
101-
102101save_city_zip_code(city, zip_code)
103102```
104103
105104** Good** :
106105
107106Decrease dependence on regex by naming subpatterns.
108- ``` php
109- $ address = 'One Infinite Loop, Cupertino 95014';
110- $cityZipCodeRegex ='/ ^[^,\\]+[,\\\s]+(?<city >.+?)\s*(?< zipCode >\d{5})?$/';
111- preg_match($cityZipCodeRegex, $ address, $matches);
107+ ``` python
108+ address= ' One Infinite Loop, Cupertino 95014'
109+ city_zip_code_regex = r ' ^ [^ , \\ ]+ [, \\ \s ]+ ( ?P <city> . +? ) \s * ( ?P<zip_code > \d {5} ) ? $ '
110+ matches = re.match(city_zip_code_regex, address)
112111
113- saveCityZipCode($ matches['city'],$ matches['zipCode ']);
112+ save_city_zip_code( matches[' city' ], matches[' zip_code ' ])
114113```
115114** [ ⬆ back to top] ( #table-of-contents ) **
116115