From e5bf2487621a55ff9e1c47d8c8fe141fd120ea9a Mon Sep 17 00:00:00 2001 From: Chris Dryden Date: Fri, 30 Jan 2026 10:39:09 -0500 Subject: [PATCH] expr: fix regex matching on inputs containing newlines (#10543) --- src/uu/expr/src/syntax_tree.rs | 6 +++--- tests/by-util/test_expr.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 172b71d33..3cfd2c83f 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -354,7 +354,7 @@ fn build_regex(pattern_bytes: Vec) -> ExprResult<(Regex, String)> { // For UTF-8 locale, use UTF-8 encoding Regex::with_options_and_encoding( &re_string, - RegexOptions::REGEX_OPTION_SINGLELINE, + RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE, Syntax::grep(), ) } @@ -362,7 +362,7 @@ fn build_regex(pattern_bytes: Vec) -> ExprResult<(Regex, String)> { // For non-UTF-8 locale, use ASCII encoding Regex::with_options_and_encoding( EncodedBytes::ascii(re_string.as_bytes()), - RegexOptions::REGEX_OPTION_SINGLELINE, + RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE, Syntax::grep(), ) } @@ -427,7 +427,7 @@ fn find_match(regex: Regex, re_string: String, left_bytes: Vec) -> ExprResul // Need to create ASCII version of regex too let re_ascii = Regex::with_options_and_encoding( EncodedBytes::ascii(re_string.as_bytes()), - RegexOptions::REGEX_OPTION_SINGLELINE, + RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE, Syntax::grep(), ) .ok(); diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index adf1cd4fd..ec1cf9159 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -457,6 +457,14 @@ fn test_regex_range_quantifier() { .stderr_only("expr: Invalid content of \\{\\}\n"); } +#[test] +fn test_regex_newline() { + new_ucmd!() + .args(&["line1\nline2\nline3 ", ":", ".*line2.*"]) + .succeeds() + .stdout_only("18\n"); +} + #[test] fn test_substr() { new_ucmd!()