Skip to content

Commit e5bf248

Browse files
authored
expr: fix regex matching on inputs containing newlines (#10543)
1 parent 12193c5 commit e5bf248

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/uu/expr/src/syntax_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,15 @@ fn build_regex(pattern_bytes: Vec<u8>) -> ExprResult<(Regex, String)> {
354354
// For UTF-8 locale, use UTF-8 encoding
355355
Regex::with_options_and_encoding(
356356
&re_string,
357-
RegexOptions::REGEX_OPTION_SINGLELINE,
357+
RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE,
358358
Syntax::grep(),
359359
)
360360
}
361361
UEncoding::Ascii => {
362362
// For non-UTF-8 locale, use ASCII encoding
363363
Regex::with_options_and_encoding(
364364
EncodedBytes::ascii(re_string.as_bytes()),
365-
RegexOptions::REGEX_OPTION_SINGLELINE,
365+
RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE,
366366
Syntax::grep(),
367367
)
368368
}
@@ -427,7 +427,7 @@ fn find_match(regex: Regex, re_string: String, left_bytes: Vec<u8>) -> ExprResul
427427
// Need to create ASCII version of regex too
428428
let re_ascii = Regex::with_options_and_encoding(
429429
EncodedBytes::ascii(re_string.as_bytes()),
430-
RegexOptions::REGEX_OPTION_SINGLELINE,
430+
RegexOptions::REGEX_OPTION_SINGLELINE | RegexOptions::REGEX_OPTION_MULTILINE,
431431
Syntax::grep(),
432432
)
433433
.ok();

tests/by-util/test_expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,14 @@ fn test_regex_range_quantifier() {
457457
.stderr_only("expr: Invalid content of \\{\\}\n");
458458
}
459459

460+
#[test]
461+
fn test_regex_newline() {
462+
new_ucmd!()
463+
.args(&["line1\nline2\nline3 ", ":", ".*line2.*"])
464+
.succeeds()
465+
.stdout_only("18\n");
466+
}
467+
460468
#[test]
461469
fn test_substr() {
462470
new_ucmd!()

0 commit comments

Comments
 (0)