Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex dot/backslash issue #206

Merged
merged 6 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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