regex 使用Python正则表达式替换字符串[重复]

vfh0ocws  于 2023-06-30  发布在  Python
关注(0)|答案(1)|浏览(128)

此问题已在此处有答案

How to replace only part of the match with python re.sub(6个答案)
5天前关闭。
我有一个多行的文件,有一个示例行如下:

create or replace view TEMP_VIEW_NAME (CODE, ASSIGN#, PHONE_NUMBER#, CUSTOMER_NUMBER, CUSTOMER_ADD, CUSTOMER_PIN#) ......

我们想用双引号替换以Hash结尾的字符串。

i = create or replace view TEMP_VIEW_NAME (CODE, "ASSIGN#", "PHONE_NUMBER#", CUSTOMER_NUMBER, CUSTOMER_ADD, "CUSTOMER_PIN#") ...... ......

已经尝试了下面的python示例版本,但它抛出错误:

file_name = "test_view.sql"

df = open(file_name, 'r')
lines = df.readlines()
for i in lines:
    linex = re.sub(r', [A-Z]_+#', '", [A-Z]_+#"', i)   #Throwing error here
    print(linex)

相关问题