I missing something very basic here.
I want to match all instances of, for instance, throw 'some string'
or throw "error here"
.
p = re.compile(b'throw ["|\'](?P<err>).*["|\']')
This seems to work fine for matching. But, for instance, I want to replace throw 'some string'
with, for instance, throw new Error('some string')
.
My attempt at this:
p.sub(rb"throw new Error('\g<err>')", b'throw \'foobar\'')
Always results in:
b"throw new Error('')"
I have found the match but replaced err
with an empty string.
答案 0 :(得分:4)
This matches and captures the empty string, followed by zero or more characters which are not captured:
(?P<err>).*
You want to move the .*
inside the parentheses:
(?P<err>.*)