我有以下形式的字符串
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 :(得分:1)
或者您可以为此使用正则表达式。
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
答案 1 :(得分:0)
一个想法是,您可以创建一个变量nested
=0。当它第一次遇到SET(
时,您可以将nested
加1。接下来,只要有一个{{1} }您将其递增1,然后在存在“)”时递减1。到(
时,您知道')'是关闭nested == 0
为达到输出目的,仅在SET(
等于nested
时输出。当0
时,表示它位于nested > 0