Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

sed - Why are there extra parenthesis in this regex substitution?

I have code with lines that look like this:

self.request.sendall(some_string)

I want to replace them to look like this:

self.request.sendall(bytes(some_string, 'utf-8'))

This is my current sed command:

sed -i "s/.sendall((.*))/.sendall(bytes(1, 'utf-8'))/g" some_file.py

I'm close, but this is the result I'm getting:

self.request.sendall(bytes((some_string), 'utf-8'))

I can't figure out where the extra open and close parenthesis are coming from in the group substitution. Does anyone see it? The escaped parenthesis are literal and need to be there. The ones around .* are to form a group for later replacement, but it's like they are becoming part of the matched text.

question from:https://stackoverflow.com/questions/65829132/why-are-there-extra-parenthesis-in-this-regex-substitution

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You escaped the wrong set of parentheses, you need to use

sed -i "s/.sendall((.*))/.sendall(bytes(1, 'utf-8'))/g" some_file.py

Note: the regex flavor you are using is POSIX BRE, thus,

  • Capturing groups are set with (...)
  • Literal parentheses are defined with mere ( and ) chars with no escapes
  • Parentheses and a dot in the RHS, replacement, are redundant.

Pattern details:

  • .sendall( - a .sendall( string
  • (.*) - Group 1 (1): any zero or more chars
  • ) - a ) char
  • .sendall(bytes(1, 'utf-8')) - RHS, where 1 refers to the Group 1 value.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...