Skip to content

Commit c09fea7

Browse files
authored
Update README.md
1 parent bb2ea04 commit c09fea7

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ time.sleep(SECONDS_IN_A_DAY)
8282
**Bad:**
8383
```python
8484
address = 'One Infinite Loop, Cupertino 95014'
85-
city_zip_code_regex = '^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'
85+
city_zip_code_regex = r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'
8686
matches = re.match(city_zip_code_regex, address)
8787

8888
save_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
9696
address = 'One Infinite Loop, Cupertino 95014'
97-
city_zip_code_regex = '^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'
97+
city_zip_code_regex = r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'
9898
matches = re.match(city_zip_code_regex, address)
9999

100100
city, zip_code = matches.groups()
101-
102101
save_city_zip_code(city, zip_code)
103102
```
104103

105104
**Good**:
106105

107106
Decrease 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

0 commit comments

Comments
 (0)