Kon*_*iot 6 python regex string
string="file()(function)(hii)out(return)(byee)"
Run Code Online (Sandbox Code Playgroud)
对于这个字符串,我需要像这样的输出
file()()()out()()
Run Code Online (Sandbox Code Playgroud)
我试过这个
string="file()(function)(hii)out(return)(byee)"
string1=re.sub("[\(\[].*?[\)\]]", "", string)
string2=re.sub(r" ?\([^)]+\)", "", string)
print(string1)
print(string2)
Run Code Online (Sandbox Code Playgroud)
并得到类似的输出
fileout
file()out
Run Code Online (Sandbox Code Playgroud)
我想要的输出应该是这样的:
file()()()out()()
Run Code Online (Sandbox Code Playgroud)
使用正则表达式:捕获括号之间的所有内容,即(.*?)并将其替换为空字符串即( )
import re
x = "file()(function)(hii)out(return)(byee)"
x = re.sub("\(.*?\)", "()", x)
print(x)
Run Code Online (Sandbox Code Playgroud)
这将打印
文件()()()输出()()