expr: fix regex matching on inputs containing newlines (#10543)

This commit is contained in:
Chris Dryden
2026-01-30 10:39:09 -05:00
committed by GitHub
parent 12193c52c6
commit e5bf248762
2 changed files with 11 additions and 3 deletions
+3 -3
View File
@@ -354,7 +354,7 @@ fn build_regex(pattern_bytes: Vec<u8>) -> 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<u8>) -> 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<u8>) -> 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();
+8
View File
@@ -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!()