Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc09fea7

Browse files
authored
Update README.md
1 parentbb2ea04 commitc09fea7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

‎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)

[8]ページ先頭

©2009-2025 Movatter.jp