re.sub escape repl

文章探讨了在Python的re.sub函数中,是否需要在替换字符串前加上'r'前缀的问题。内容指出,在某些情况下,不加'r'可能导致字符串转义问题,而加上'r'可以确保原始字符串不被解释。通过示例代码解释了'r'的作用,强调了在处理特殊字符时使用'r'的必要性。
import os import subprocess import re import_library_name_prefix = r'tcbn07_bwph240l11p57pd_base_lvt' library_name_prefix = "H3STC6TP57G11LBA01" def apply_sed_commands(file_path, cmds): for cmd in cmds: subprocess.run(['sed', '-i', cmd, file_path], check=True) def process_lib_file(file_path, library_name_prefix, import_library_name_prefix): file_name = os.path.basename(file_path) prefix = re.search(import_library_name_prefix, file_name) prefix_str = prefix.group() if prefix else "" match = re.search( rf'{import_library_name_prefix}' r'([a-zA-Z0-9]+_[a-zA-Z0-9]+_[a-zA-Z0-9]+)' r'(?:_.*?)?\.lib', file_name ) suffix_str = "" if match: tmp = match.group(1) tmp = re.sub(r'([0-9])p([0-9]+)v', r'\1P\2V', tmp) tmp = re.sub(r'([0-9]+)c', r'\1C', tmp) clean_suffix = re.sub(r'[^a-zA-Z0-9]', '', tmp).upper() suffix_str = clean_suffix original_suffix_pattern = re.escape(match.group(1)) sed_commands = [ '1,41d', '/date/d', '/comment/d', '/revision/d', r's/\(nom_process.*\);.*/\1;/', r's/\(process.*\);.*/\1;/', 's/ZeroWireload/wl0/g', '/slew_upper_threshold_pct_fall/,/leakage_power_unit/{//!d}', r's/^\s*\(leakage_power_unit\)/\1/', r's/^\s*\(default_cell_leakage_power\)/\1/', '84,88d', '/Design/d', '/\/\* ---------/d', '/--------- \*\//d', f's/{re.escape(prefix_str)}/{re.escape(library_name_prefix)}/g', f's/{original_suffix_pattern}/{clean_suffix}/g', f's/{re.escape(library_name_prefix + clean_suffix)}/{re.escape(library_name_prefix)}_{clean_suffix}/g', 's/_cbest_CCbest_T//g', 's/_cworst_CCworst_T//g', 's/_typical//g', 's/VM40C/VN40C/g', ] apply_sed_commands(file_path, sed_commands) if __name__ == "__main__": for lib_file in [f for f in os.listdir('.') if f.lower().endswith('.lib')]: print(f"Start to update lib : {lib_file}") process_lib_file(lib_file, library_name_prefix, import_library_name_prefix) 详细解释上述代码的含义,包括每个正则表达式的字符含义
11-28
File "D:\anaconda3\envs\pytorch\TCL-master\TCL-master\main.py", line 23, in <module> from models.basic_template import TrainTask File "D:\anaconda3\envs\pytorch\TCL-master\TCL-master\models\__init__.py", line 8, in <module> import_models(osp.dirname(__file__), 'models') File "D:\anaconda3\envs\pytorch\TCL-master\TCL-master\utils\model_register.py", line 97, in import_models lib = re.sub(root, prefix, p) ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\__init__.py", line 186, in sub return _compile(pattern, flags).sub(repl, string, count) ^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\__init__.py", line 307, in _compile p = _compiler.compile(pattern, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\_compiler.py", line 750, in compile p = _parser.parse(p, flags) ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\_parser.py", line 979, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\_parser.py", line 460, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\_parser.py", line 544, in _parse code = _escape(source, this, state) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda3\envs\pytorch\Lib\re\_parser.py", line 443, in _escape raise source.error("bad escape %s" % escape, len(escape)) re.error: bad escape \e at position 12
11-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值