Skip to content

Commit

Permalink
Fix regex dot/backslash issue (#206)
Browse files Browse the repository at this point in the history
* fix regex backslash

* fix regex backslash

* fix regex backslash

* run make cs
  • Loading branch information
krsriq committed Dec 21, 2020
1 parent 830891d commit 3328cb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,21 @@ public static function regexify($regex = '')
// replace \d with number and \w with letter and . with ascii
$regex = preg_replace_callback('/\\\w/', 'static::randomLetter', $regex);
$regex = preg_replace_callback('/\\\d/', 'static::randomDigit', $regex);
$regex = preg_replace_callback('/(?<!\\\)\./', 'static::randomAscii', $regex);
// remove remaining backslashes
//replace . with ascii except backslash
$regex = preg_replace_callback('/(?<!\\\)\./', static function () {
$chr = static::asciify('*');

if ($chr === '\\') {
$chr .= '\\';
}

return $chr;
}, $regex);
// remove remaining single backslashes
$regex = str_replace('\\\\', '[:escaped_backslash:]', $regex);
$regex = str_replace('\\', '', $regex);
$regex = str_replace('[:escaped_backslash:]', '\\', $regex);

// phew
return $regex;
}
Expand Down
1 change: 1 addition & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public function regexifyDataProvider()
['\.\*\?\+', 'escaped characters'],
['[.]', 'literal dot in character class'],
['.', 'catch-all dot'],
['\\\\', 'escaped backslash'],
['[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}', 'complex regex'],
];
}
Expand Down

0 comments on commit 3328cb9

Please sign in to comment.