我想对下面的代码应用正则表达式,这样我就可以删除出现在逗号和单词'AS'之间的任何字符串。
Select customer_name, customer_type, COUNT(*) AS volumenFROM tablenGROUP BY customer_name, customer_typenORDER BY volume DESCnLIMIT 10
预期输出:
Select customer_name, customer_type, volumenFROM tablenGROUP BY customer_name, customer_typenORDER BY volume DESCnLIMIT 10
我尝试了下面的命令,但是没有得到想要的输出
result = re.sub(r",s*COUNT(*)s*ASs*w+", "", text)
您可以使用捕获组并在替换中使用该组。
(,s*)[^,]*sASbs*
(,s*)
捕获组1,匹配逗号和可选的空白字符[^,]*
匹配除逗号以外的任何字符sASbs*
匹配一个空白字符,然后是AS
,后跟可选的空格
Regex demo | Python demo
import re
pattern = r"(,s*)[^,]*sASbs*"
s = ("Select customer_name, customer_type, COUNT(*) AS volume\nFROM table\nGROUP BY customer_name, customer_type\nORDER BY volume DESC\nLIMIT 10n")
print(re.sub(pattern, r"1", s))
输出Select customer_name, customer_type, volumenFROM tablenGROUP BY customer_name, customer_typenORDER BY volume DESCnLIMIT 10
我将使用:
text = "Select customer_name, customer_type, COUNT(*) AS volumenFROM tablenGROUP BY customer_name, customer_typenORDER BY volume DESCnLIMIT 10"
result = re.sub(r',s*S+s+ASbs*', ', ', text)
print(result)
这个打印:
Select customer_name, customer_type, volume
FROM table
GROUP BY customer_name, customer_type
ORDER BY volume DESC
LIMIT 10
这里使用的正则表达式模式表示匹配:
,
a逗号s*
可选空白S+
a非空白项s+
一个或多个空白字符AS
literal "AS">b
字边界s*
更多可选空格