Python 从文件中删除特定模式
我有一个字符串的形式Python 从文件中删除特定模式,python,Python,我有一个字符串的形式 HELLO SET("ui_mapping_text"='#cast( #sum(cast(STG_HO.TOTAL_ORDER,\'decimal(8,0)\')-cast(STG_HO.TAX,\'decimal(8,0)\'))/100 #+ #sum(cast(STG_HO.TOTAL_DISCOUNT,\'decimal(8,0)\'))/100 #+ #sum(cast(STG_HO.GST_DISCOUNT,
HELLO SET("ui_mapping_text"='#cast(
#sum(cast(STG_HO.TOTAL_ORDER,\'decimal(8,0)\')-cast(STG_HO.TAX,\'decimal(8,0)\'))/100
#+
#sum(cast(STG_HO.TOTAL_DISCOUNT,\'decimal(8,0)\'))/100
#+
#sum(cast(STG_HO.GST_DISCOUNT,\'decimal(8,0)\'))/100, \'decimal(10,2)\')
cast(
sum(cast(STG_HO.TOTAL_ORDER,\'decimal(8,0)\')-cast(STG_HO.TAX,\'decimal(8,0)\'))/100
+
sum(cast(STG_HO.TOTAL_DISCOUNT,\'decimal(8,0)\'))/100
+
sum(cast(nvl(STG_HO.GST_DISCOUNT,0),\'decimal(8,0)\'))/100, \'decimal(10,2)\')')
HELLO
我想从python文件中删除SET()和SET本身之间的任何内容。需要进行毕业论文匹配
预期产量
HELLO
HELLO
一个想法是,您可以创建一个嵌套的变量
=0。当它第一次遇到SET(
)时,你将nested
增加1。接下来,每当有(
你将它增加1,当有一个“')时,你将其减少1。当nested==0
时,你知道“')是关闭集(
为了达到输出的目的,您只能在
nested
等于0
时输出。当nested>0
时,这意味着它位于集合(…)
内,或者您可以使用regex进行此操作
txt = r"""
HELLO SET("ui_mapping_text"='#cast(
#sum(cast(STG_HO.TOTAL_ORDER,\'decimal(8,0)\')-cast(STG_HO.TAX,\'decimal(8,0)\'))/100
#+
#sum(cast(STG_HO.TOTAL_DISCOUNT,\'decimal(8,0)\'))/100
#+
#sum(cast(STG_HO.GST_DISCOUNT,\'decimal(8,0)\'))/100, \'decimal(10,2)\')
cast(
sum(cast(STG_HO.TOTAL_ORDER,\'decimal(8,0)\')-cast(STG_HO.TAX,\'decimal(8,0)\'))/100
+
sum(cast(STG_HO.TOTAL_DISCOUNT,\'decimal(8,0)\'))/100
+
sum(cast(nvl(STG_HO.GST_DISCOUNT,0),\'decimal(8,0)\'))/100, \'decimal(10,2)\')')
HELLO
"""
import re
result = re.sub(r"SET\((?s).*\)","",txt)
print (result)
结果:
HELLO
HELLO
不执行(匹配)。将在SET()与您的数据完全按照上面所示运行benn closedIt之后删除这些。如果您有其他要求,您应该更新您的问题。您已经提到原始问题中需要parenthsis匹配。无论如何,谢谢