Fix \?, \+ and \| to be treated as ERE in BRE mode

This commit is contained in:
LoukasPap
2026-04-30 22:05:25 +02:00
committed by Sylvestre Ledru
parent 6215664b2e
commit b7072cb4a5
5 changed files with 65 additions and 2 deletions
+14 -2
View File
@@ -533,7 +533,7 @@ fn parse_command_ending(
}
/// Convert a primitive BRE pattern to a safe ERE-compatible pattern string.
/// - Replaces `\(` and `\)` with `(` and `)`.
/// - Replaces `\(`, `\)`, `\?`, `\+` and `\|` with `(`, `)`, `?`, `+` and `|`.
/// - Puts single-digit back-references in non-capturing groups..
/// - Escapes ERE-only metacharacters: `+ ? { } | ( )`.
/// - Leaves all other characters as-is.
@@ -554,6 +554,18 @@ fn bre_to_ere(pattern: &str) -> String {
chars.next();
result.push(')'); // Group end
}
Some('?') => {
chars.next();
result.push('?'); // Quantifier 0 or 1
}
Some('+') => {
chars.next();
result.push('+'); // Quantifier 1 or more
}
Some('|') => {
chars.next();
result.push('|'); // Alternation operator
}
Some(v) if v.is_ascii_digit() => {
// Back-reference. In sed BREs these are single-digit
// (\1-\9) whereas fancy_regex supports multi-digit
@@ -2192,7 +2204,7 @@ mod tests {
// bre_to_ere
#[test]
fn test_bre_group_translation() {
assert_eq!(bre_to_ere(r"\(abc\)"), "(abc)");
assert_eq!(bre_to_ere(r"\(a\?b\+c\|\)"), "(a?b+c|)");
assert_eq!(bre_to_ere(r"a\(b\)c"), "a(b)c");
}
+9
View File
@@ -300,6 +300,15 @@ check_output!(
LINES1
]
);
check_output!(
subst_quantifier_zero_or_one,
["-e", r"s/_[0-9]\([0-9]\)\?/_x\1/g", LINES1]
);
check_output!(
subst_quantifier_one_or_more,
["-e", r"s/_[0-9]\+/_x/g", LINES1]
);
check_output!(subst_alternation_operator, ["-e", r"s/0\|1/x/g", LINES1]);
check_output!(subst_multiline, ["-e", "s/_/u0\\\nu1\\\nu2/g", LINES1]);
check_output!(subst_numbered_replacement, ["-e", r"s/./X/4", LINES1]);
check_output!(subst_brace, ["-e", r"s/[123]/X/g", LINES1]);
+14
View File
@@ -0,0 +1,14 @@
lx_x
lx_2
lx_3
lx_4
lx_5
lx_6
lx_7
lx_8
lx_9
lx_xx
lx_xx
lx_x2
lx_x3
lx_x4
+14
View File
@@ -0,0 +1,14 @@
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
+14
View File
@@ -0,0 +1,14 @@
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x0
l1_x1
l1_x2
l1_x3
l1_x4