Skip to content

Commit dc988fe

Browse files
authored
Fix regexify escape backslash in character class (#434)
* TASK: fix regexify backslash in character class
1 parent 65ede06 commit dc988fe

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Diff for: src/Faker/Provider/Base.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ public static function regexify($regex = '')
525525
}, $regex);
526526
// All [ABC] become B (or A or C)
527527
$regex = preg_replace_callback('/\[([^\]]+)\]/', static function ($matches) {
528-
$randomElement = Base::randomElement(str_split($matches[1]));
528+
// remove backslashes (that are not followed by another backslash) because they are escape characters
529+
$match = preg_replace('/\\\(?!\\\)/', '', $matches[1]);
530+
$randomElement = Base::randomElement(str_split($match));
529531
//[.] should not be a random character, but a literal .
530532
return str_replace('.', '\.', $randomElement);
531533
}, $regex);

Diff for: test/Faker/Provider/BaseTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ public function regexifyDataProvider()
333333
['[aeiou]', 'basic character class'],
334334
['[a-z]', 'character class range'],
335335
['[a-z1-9]', 'multiple character class range'],
336+
['[a-z\-]{4}', 'character class range with quantifier and escaped character'],
336337
['a*b+c?', 'single character quantifiers'],
337338
['a{2}', 'brackets quantifiers'],
338339
['a{2,3}', 'min-max brackets quantifiers'],

0 commit comments

Comments
 (0)