Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文件中删除特定模式_Python - Fatal编程技术网

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匹配。无论如何,谢谢