1

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.

1 Answer 1

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>.*)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.