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 regexify escape backslash in character class #434

Merged
merged 3 commits into from
Jan 28, 2022
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
4 changes: 3 additions & 1 deletion src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ public static function regexify($regex = '')
}, $regex);
// All [ABC] become B (or A or C)
$regex = preg_replace_callback('/\[([^\]]+)\]/', static function ($matches) {
$randomElement = Base::randomElement(str_split($matches[1]));
// remove backslashes (that are not followed by another backslash) because they are escape characters
$match = preg_replace('/\\\(?!\\\)/', '', $matches[1]);
$randomElement = Base::randomElement(str_split($match));
//[.] should not be a random character, but a literal .
return str_replace('.', '\.', $randomElement);
}, $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 @@ -333,6 +333,7 @@ public function regexifyDataProvider()
['[aeiou]', 'basic character class'],
['[a-z]', 'character class range'],
['[a-z1-9]', 'multiple character class range'],
['[a-z\-]{4}', 'character class range with quantifier and escaped character'],
['a*b+c?', 'single character quantifiers'],
['a{2}', 'brackets quantifiers'],
['a{2,3}', 'min-max brackets quantifiers'],
Expand Down