Skip to content

Commit fae62ee

Browse files
committed
expr: Handle caret '^' at the beginning of a capturing group
1 parent 8866ec3 commit fae62ee

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ impl StringOp {
171171
let mut prev_is_escaped = false;
172172
for curr in pattern_chars {
173173
match curr {
174-
// Carets are interpreted literally, unless used as character class negation "[^a]"
175-
'^' if prev_is_escaped || !matches!(prev, '\\' | '[') => {
176-
re_string.push_str(r"\^");
177-
}
178-
char => re_string.push(char),
174+
'^' => match (prev, prev_is_escaped) {
175+
// Start of a capturing group
176+
('(', true) => re_string.push(curr),
177+
// Character class negation "[^a]"
178+
('[', false) => re_string.push(curr),
179+
// Explicitly escaped caret
180+
('\\', false) => re_string.push(curr),
181+
_ => re_string.push_str(r"\^"),
182+
},
183+
_ => re_string.push(curr),
179184
}
180185

181186
prev_is_escaped = prev == '\\' && !prev_is_escaped;

0 commit comments

Comments
 (0)