Skip to content

Commit

Permalink
Fix regexify escape backslash in character class (#434)
Browse files Browse the repository at this point in the history
* TASK: fix regexify backslash in character class
  • Loading branch information
krsriq committed Jan 28, 2022
1 parent 65ede06 commit dc988fe
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Faker/Provider/Base.php
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
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

0 comments on commit dc988fe

Please sign in to comment.