From a53edae8ad52aa8a4efcfd1f7be4fdbe7659dfa2 Mon Sep 17 00:00:00 2001 From: Mike Kasberg Date: Sat, 9 Dec 2017 13:13:36 -0700 Subject: [PATCH] Fix #2907: Allow Escaped % in String Format Match For example, the format '%%d' should match the string literal '%d'. We achieve this by using negative lookbehind (http://php.net/manual/en/regexp.reference.assertions.php) when constructing the regex pattern that we will match against. --- .../StringMatchesFormatDescription.php | 26 ++++++++++--------- .../StringMatchesFormatDescriptionTest.php | 5 ++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Framework/Constraint/StringMatchesFormatDescription.php b/src/Framework/Constraint/StringMatchesFormatDescription.php index b146c15ab7e..59a9b89cd52 100644 --- a/src/Framework/Constraint/StringMatchesFormatDescription.php +++ b/src/Framework/Constraint/StringMatchesFormatDescription.php @@ -66,19 +66,19 @@ protected function additionalFailureDescription($other) protected function createPatternFromFormat($string) { - $string = \str_replace( + $string = \preg_replace( [ - '%e', - '%s', - '%S', - '%a', - '%A', - '%w', - '%i', - '%d', - '%x', - '%f', - '%c' + '/(? [ + true, + 'Escaped %%e %%s %%S %%a %%A %%w %%i %%d %%x %%f %%c', + 'Escaped %e %s %S %a %A %w %i %d %x %f %c' ] ]; }