extmod/modure: Allow \\ in re.sub replacements.
This commit is contained in:
parent
0fd0eb00aa
commit
319437d4bd
@ -343,6 +343,9 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
|
|||||||
const char *end_match = match->caps[match_no * 2 + 1];
|
const char *end_match = match->caps[match_no * 2 + 1];
|
||||||
vstr_add_strn(&vstr_return, start_match, end_match - start_match);
|
vstr_add_strn(&vstr_return, start_match, end_match - start_match);
|
||||||
}
|
}
|
||||||
|
} else if (*repl == '\\') {
|
||||||
|
// Add the \ character
|
||||||
|
vstr_add_byte(&vstr_return, *repl++);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Just add the current byte from the replacement string
|
// Just add the current byte from the replacement string
|
||||||
|
@ -43,6 +43,9 @@ print(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# \g immediately followed by another \g
|
||||||
|
print(re.sub("(abc)", r"\g<1>\g<1>", "abc"))
|
||||||
|
|
||||||
# no matches at all
|
# no matches at all
|
||||||
print(re.sub("a", "b", "c"))
|
print(re.sub("a", "b", "c"))
|
||||||
|
|
||||||
@ -69,3 +72,6 @@ try:
|
|||||||
re.sub(123, "a", "a")
|
re.sub(123, "a", "a")
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print("TypeError")
|
print("TypeError")
|
||||||
|
|
||||||
|
# Include \ in the sub replacement
|
||||||
|
print(re.sub("b", "\\\\b", "abc"))
|
||||||
|
Loading…
Reference in New Issue
Block a user