从fi中删除特定图案

2024-05-12 21:43:26 发布

您现在位置: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,\'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

Tags: 字符串uihelloorderdiscount形式totaldecimal
2条回答

或者你可以用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

一个想法是,可以创建一个变量nested=0。当它第一次遇到SET(时,您将nested增加1。接下来,每当有(时,您将它增加1,当有''时,将它减少1。到nested == 0时,您就知道“)”是关闭SET(

为了达到输出的目的,只有当nested等于0时才输出。当nested > 0时,表示它在SET(...)

相关问题 更多 >