regex - python re.sub missing group name error -
i'm going crazy have no idea why i'm getting error:
traceback (most recent call last): file "file.py", line 154, in <module> main() file "file.py", line 143, in main dld = work() file "file.py", line 100, in work string1= re.sub(find_dev_line, dev_line_to_replace, string1) file "c:\program files (x86)\python27\lib\re.py", line 151, in sub return _compile(pattern, flags).sub(repl, string, count) file "c:\program files (x86)\python27\lib\re.py", line 270, in _subx template = _compile_repl(template, pattern) file "c:\program files (x86)\python27\lib\re.py", line 257, in _compile_repl raise error, v # invalid expression sre_constants.error: missing group name
with these lines of code:
dev_line_searchterm = "^.+"+devname+".+$" dev_line_to_replace = str(os.path.join(start_dir, "downloads", devname, filename)) find_dev_line = re.compile(dev_line_searchterm, re.multiline) string1= re.sub(find_dev_line, dev_line_to_replace, string1)
i dont have groups in these lines, why getting error?
dev_line_to_replace
contains special characters have meaning regex engine. should escape devname
, dev_line_to_replace
using re.escape
, avoid having special characters appear in search or replace patterns.
Comments
Post a Comment