Skip to content

Commit c56681e

Browse files
authored
Merge pull request zedr#19 from peter-gribanov/explanatory_variables
[Enhancement] Decrease dependence on regex
2 parents ce41bd3 + c806ccd commit c56681e

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ preg_match($cityZipCodeRegex, $address, $matches);
8989
saveCityZipCode($matches[1], $matches[2]);
9090
```
9191

92-
**Good**:
92+
**Not bad**:
93+
94+
It's better, but we are still heavily dependent on regex.
95+
9396
```php
9497
$address = 'One Infinite Loop, Cupertino 95014';
9598
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
@@ -98,6 +101,17 @@ preg_match($cityZipCodeRegex, $address, $matches);
98101
list(, $city, $zipCode) = $matchers;
99102
saveCityZipCode($city, $zipCode);
100103
```
104+
105+
**Good**:
106+
107+
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);
112+
113+
saveCityZipCode($matchers['city'], $matchers['zipCode']);
114+
```
101115
**[⬆ back to top](#table-of-contents)**
102116

103117
### Avoid Mental Mapping

0 commit comments

Comments
 (0)