TIL 131 - Change casing in search & replace
Briefly

VS Code search-and-replace supports regular expressions to capture groups and special escape sequences to modify casing of inserted groups. The sequences \U and \L convert an inserted group to all upper- or lower-case, while \u and \l change only the first character to upper- or lower-case. Python's re.sub does not provide these special casing sequences directly. re.sub accepts a callable replacement, so a replacement function can inspect match.group(0) and return transformed text using .upper(), .lower(), or by altering the first character to emulate \u and \l behavior.
VS Code has a search & replace feature that lets you use regex to look for patterns and then reference groups in the replacement... But it lets you do something else that's really cool. Changing casing with special sequences When you are replacing groups, you can use special sequences to change the casing of the group you're inserting, according to the following table: The picture below shows an example of a search & replace operation where I looked for the text "all in one go".
The module re in Python supports searching and replacing with the function re.sub but it doesn't let you do the same case-changing operations with special sequences. Instead, you have to use the fact thatre.sub supports dynamic string replacements and then implement the logic yourself. First, you can't use the string methods upper and lower directly for \U and \L; you have to grab the text from the object Match. You also have to pick the string apart to implement the\u and \l:
Read at Mathspp
[
|
]