Rodrigo Girão Serrão: TIL #131 – Change casing in search & replace
VS Code's search and replace functionality allows for regex-based pattern matching and group referencing in replacements. A powerful feature within this is the ability to change the casing of matched groups using special sequences. The sequences include \u for uppercasing the first letter, \U for uppercasing the entire group, \l for lowercasing the first letter, and \L for lowercasing the entire group. For instance, replacing "all in one go" with \U$1 transforms it into "ALL IN ONE GO". In contrast, Python's re.sub function, while supporting dynamic replacements, does not offer these built-in special sequences for case manipulation. To achieve similar results in Python, one must implement custom functions that interact with the matched text object. These Python functions, like all_upper and first_upper, mimic the behavior of VS Code's special sequences by programmatically altering the case of the matched string. The all_upper function converts the entire matched group to uppercase, corresponding to VS Code's \U. Similarly, first_upper capitalizes only the initial character of the match, mirroring VS Code's \u. The Python module requires manual implementation of these case changes, unlike the direct special sequences provided by VS Code.
re.subfunction, while supporting dynamic replacements, does not offer these built-in special sequences for case manipulation. To achieve similar results in Python, one must implement custom functions that interact with the matched text object. These Python functions, likeall_upperandfirst_upper, mimic the behavior of VS Code's special sequences by programmatically altering the case of the matched string. Theall_upperfunction converts the entire matched group to uppercase, corresponding to VS Code's \U. Similarly,first_uppercapitalizes only the initial character of the match, mirroring VS Code's \u. The Python module requires manual implementation of these case changes, unlike the direct special sequences provided by VS Code.