Skip to content

Commit 2d8720c

Browse files
author
Tomáš Votruba
authored
Merge pull request piotrplenik#125 from peter-gribanov/cityZipCodeRegex
Optimize City and ZipCode regex
2 parents 3309c09 + dc125f0 commit 2d8720c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ if ($user->access & User::ACCESS_UPDATE) {
151151

152152
```php
153153
$address = 'One Infinite Loop, Cupertino 95014';
154-
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
154+
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
155155
preg_match($cityZipCodeRegex, $address, $matches);
156156

157157
saveCityZipCode($matches[1], $matches[2]);
@@ -163,7 +163,7 @@ It's better, but we are still heavily dependent on regex.
163163

164164
```php
165165
$address = 'One Infinite Loop, Cupertino 95014';
166-
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
166+
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
167167
preg_match($cityZipCodeRegex, $address, $matches);
168168

169169
[, $city, $zipCode] = $matches;
@@ -176,7 +176,7 @@ Decrease dependence on regex by naming subpatterns.
176176

177177
```php
178178
$address = 'One Infinite Loop, Cupertino 95014';
179-
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(?<city>.+?)\s*(?<zipCode>\d{5})?$/';
179+
$cityZipCodeRegex = '/^[^,]+,\s*(?<city>.+?)\s*(?<zipCode>\d{5})$/';
180180
preg_match($cityZipCodeRegex, $address, $matches);
181181

182182
saveCityZipCode($matches['city'], $matches['zipCode']);

0 commit comments

Comments
 (0)