如何在文件中找到模式并使用python在文件中注释该模式

时间:2019-06-20 17:02:27

标签: python-3.x

我想从“ COMPRESS”到“)”搜索文件中的模式并对其进行注释。

我的输入文件如下:

CREATE MULTISET TABLE TESTDB.testTbl ,FALLBACK ,
     (
      Local_Pd BIGINT NOT NULL,
      Year_Id INTEGER NOT NULL,
      par_t CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      PB_Ind INTEGER COMPRESS(0,1,2,3,4,5,6,6))
UNIQUE PRIMARY INDEX ( Local_Pd ,Year_Id ,par_t,
       PB_Ind);

输出文件:

CREATE MULTISET TABLE TESTDB.testTbl ,FALLBACK ,
     (
      Local_Pd BIGINT NOT NULL,
      Year_Id INTEGER NOT NULL,
      par_t CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      PB_Ind INTEGER /* COMPRESS(0,1,2,3,4,5,6,6) */  )
UNIQUE PRIMARY INDEX ( Local_Pd ,Year_Id ,par_t,
       PB_Ind);

1 个答案:

答案 0 :(得分:0)

类似的事情应该起作用

import re

test_str = "CREATE MULTISET TABLE TESTDB.testTbl ,FALLBACK , ( Local_Pd BIGINT NOT NULL, Year_Id INTEGER NOT NULL, par_t CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC, PB_Ind INTEGER COMPRESS(0,1,2,3,4,5,6,6)) UNIQUE PRIMARY INDEX ( Local_Pd ,Year_Id ,par_t, PB_Ind);"

regex = r"(COMPRESS\([^\)]*\))"
t=re.sub(regex, r"/* \1 */", test_str)
print(t)