Форум сайта python.su
Пытаюсь заменить по шаблону. Строка, которой я хочу заменить шаблон, может содержать бекслеши, как один, так и несколько. Также вероятнее всего она может содержать и еще какие-то спецсимволы регулярных выражений.
>>> import re >>> category_name = 'SWITCH\\ELECTRONIC DEVICE' >>> re.sub('{{ category_name }}', category_name, 'Category name: {{ category_name }}') Traceback (most recent call last): File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/sre_parse.py", line 1039, in parse_template this = chr(ESCAPES[this][1]) KeyError: '\\E' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/stright/.pyenv/versions/some_interpreter/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-4-b3dbd0a62f2d>", line 1, in <module> re.sub('{{ category_name }}', category_name, 'Category name: {{ category_name }}') File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 210, in sub return _compile(pattern, flags).sub(repl, string, count) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 327, in _subx template = _compile_repl(template, pattern) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 318, in _compile_repl return sre_parse.parse_template(repl, pattern) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/sre_parse.py", line 1042, in parse_template raise s.error('bad escape %s' % this, len(this)) re.error: bad escape \E at position 6
>>> re.sub('{{ category_name }}', re.escape(category_name), 'Category name: {{ category_name }}') >>> 'Category name: SWITCH\\ELECTRONIC\\ DEVICE'
>>> category_name = 'SWITCH\ELECTRONIC DEVICE' >>> re.sub('{{ category_name }}', re.escape(category_name), 'Category name: {{ category_name }}') >>> 'Category name: SWITCH\\ELECTRONIC\\ DEVICE'
>>> category_name = 'SWITCH.ELECTRONIC\ DEVICE' >>> re.sub('{{ category_name }}', re.escape(category_name), 'Category name: {{ category_name }}') >>> 'Category name: SWITCH\\.ELECTRONIC\\\\ DEVICE'
'Category name: SWITCH\\ELECTRONIC DEVICE'
Отредактировано Stright (Май 27, 2021 15:15:41)
Офлайн
>>> import re >>> >>> category_name = r'SWITCH\\ELECTRONIC DEVICE' >>> re.sub(re.escape('{{ category_name }}'), category_name, 'Category name: {{ category_name }}') 'Category name: SWITCH\\ELECTRONIC DEVICE' >>>
Офлайн
>>> category_name = 'SWITCH ELECTRONIC\ DEVICE' >>> re.sub(re.escape('{{ category_name }}'), category_name, 'Category name: {{ category_name }}') >>> 'Category name: SWITCH ELECTRONIC\\ DEVICE'
Отредактировано Stright (Май 27, 2021 15:33:18)
Офлайн
И даже с сырой строкой получается ошибка в случае одного бекслеша:
>>> category_name = r'SWITCH \ELECTRONIC DEVICE' >>> re.sub(re.escape('{{ category_name }}'), category_name, 'Category name: {{ category_name }}') Traceback (most recent call last): File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/sre_parse.py", line 1039, in parse_template this = chr(ESCAPES[this][1]) KeyError: '\\E' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/stright/.pyenv/versions/some_interpreter/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-34-2f77c357ec22>", line 1, in <module> re.sub(re.escape('{{ category_name }}'), category_name, 'Category name: {{ category_name }}') File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 210, in sub return _compile(pattern, flags).sub(repl, string, count) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 327, in _subx template = _compile_repl(template, pattern) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/re.py", line 318, in _compile_repl return sre_parse.parse_template(repl, pattern) File "/home/stright/.pyenv/versions/3.9.5/lib/python3.9/sre_parse.py", line 1042, in parse_template raise s.error('bad escape %s' % this, len(this)) re.error: bad escape \E at position 7
Офлайн
Сделал так:
>>> import re >>> pattern = re.compile(r'{{ category_name }}') >>> category_name = 'SWITCH\\ELECTRONIC DEVICE' >>> s = 'Category name: {{ category_name }} {{ category_name }} some text' >>> while m := pattern.search(s): >>> start_pos, end_pos = m.span() >>> s = f'{s[:start_pos]}{category_name}{s[end_pos:]}' >>> s >>> 'Category name: SWITCH\\ELECTRONIC DEVICE SWITCH\\ELECTRONIC DEVICE some text'
Отредактировано Stright (Май 27, 2021 16:13:42)
Офлайн
Stright
И даже с сырой строкой получается ошибка в случае одного бекслеша
>>> import re >>> >>> category_name = r'SWITCH \\ELECTRONIC DEVICE' >>> re.sub(re.escape('{{ category_name }}'), category_name, 'Category name: {{ category_name }}') 'Category name: SWITCH \\ELECTRONIC DEVICE' >>> print(_) Category name: SWITCH \ELECTRONIC DEVICE >>>
Офлайн