From bdceb76316aecf1b1c01c62b581c45e7918479ce Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 1 Jun 2020 17:42:20 +0300 Subject: [PATCH] Fix matching new line character at the end of label. Closes smart-pattern/valentina#46. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because Perl returns a string with a newline at the end when reading a line from a file, Perl’s regex engine matches $ at the position before the line break at the end of the string even when multi-line mode is turned off. Perl also matches $ at the very end of the string, regardless of whether that character is a line break. So ^\d+$ matches 123 whether the subject string is 123 or 123\n. Most modern regex flavors have copied this behavior. That includes .NET, Java, PCRE, Delphi, PHP, and Python. This behavior is independent of any settings such as “multi-line mode”. In all these flavors except Python, \Z also matches before the final line break. If you only want a match at the absolute very end of the string, use \z (lowercase z instead of uppercase Z). \A\d+\z does not match 123\n. \z matches after the line break, which is not matched by the shorthand character class. --- src/libs/qmuparser/qmudef.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libs/qmuparser/qmudef.cpp b/src/libs/qmuparser/qmudef.cpp index 608c85e60..4e69a74b2 100644 --- a/src/libs/qmuparser/qmudef.cpp +++ b/src/libs/qmuparser/qmudef.cpp @@ -323,8 +323,11 @@ QString NameRegExp() //Same regexp in pattern.xsd shema file. Don't forget to synchronize. // \p{Nd} - \p{Decimal_Digit_Number} // \p{Zs} - \p{Space_Separator} - regex = QString("^([^\\p{Nd}\\p{Zs}*/&|!<>^\n\\()%1%2%3%4=?:;'\"]){1,1}" - "([^\\p{Zs}*/&|!<>^\n\\()%1%2%3%4=?:;\"]){0,}$") + // Here we use permanent start of string and end of string anchors \A and \z to match whole pattern as one + // string. In some cases, a user may pass multiline or line that ends with a new line. To cover case with a new + // line at the end of string use /z anchor. + regex = QString("\\A([^\\p{Nd}\\p{Zs}*/&|!<>^\\n\\()%1%2%3%4=?:;'\"]){1,1}" + "([^\\p{Zs}*/&|!<>^\\n\\()%1%2%3%4=?:;\"]){0,}\\z") .arg(negativeSigns, positiveSigns, decimalPoints, groupSeparators); }