Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 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 Reply

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...