替换字符串中的特定单词

时间:2018-08-22 07:39:13

标签: python python-2.7

我发现很难将字符串的单词更改为固定的单词。下面是字符串的内容

 CREATE TABLE "USER1"."TEST_TAB"
   (    "O_ID" CHAR(15),
        "CREATED_DATE" DATE DEFAULT SYSDATE,
        "EVENT_ID" NUMBER,
        "LOD" BLOB,
        "TOPIC" VARCHAR2(765),
        "SCHEMA_ID" VARCHAR2(36),
        "RECORD_ID" CHAR(15),
         CONSTRAINT "R_PKCONDUIT_MESSAGE" PRIMARY KEY ("O_ID", "TOPIC", "EVENT_ID", "CREATED_DATE") ENABLE
   ) O INDEX COMPRESS 2 PCTFREE 10 INITRANS 2 MAXTRANS 255  NOLOGGING
  TABLESPACE "D01"
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
 PCTTHRESHOLD 50
 LOB ("LOD") STORE AS BASICFILE (
  DISABLE STORAGE IN ROW CHUNK 8192 RETENTION
  NOCACHE LOGGING
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT))
  PARTITION BY HASH ("O_ID")
 (PARTITION "CONDUIT_MESSAGE01"
 LOB ("LOD") STORE AS BASICFILE "LOD01"(
  TABLESPACE "D01" )
  TABLESPACE "D01" ,
 PARTITION "CONDUIT_MESSAGE02"
 LOB ("LOD") STORE AS BASICFILE "LOD02"(
  TABLESPACE "D02" )
  TABLESPACE "D02" ,
 PARTITION "CONDUIT_MESSAGE03"
 LOB ("LOD") STORE AS BASICFILE "LOD03"(
  TABLESPACE "D03" )
  TABLESPACE "D03" ,
 PARTITION "CONDUIT_MESSAGE04"
 LOB ("LOD") STORE AS BASICFILE "LOD04"(
  TABLESPACE "D04" )
  TABLESPACE "D04" ,
 PARTITION "CONDUIT_MESSAGE05"
 LOB ("LOD") STORE AS BASICFILE "LOD05"(
  TABLESPACE "D05" )
  TABLESPACE "D05" ,
 PARTITION "CONDUIT_MESSAGE06"
 LOB ("LOD") STORE AS BASICFILE "LOD06"(
  TABLESPACE "D06" )
  TABLESPACE "D06"
/

试图将TABLESPACE“ D01” .... TABLESPACE“ D06”更改为TABLESPACE“ COPY_TBS”的固定名称

string.replace('TABLESPACE \"D[0-9][0-9]+\"','TABLESPACE COPY_TBS')

似乎不起作用。有什么办法可以改变这个字符串。

2 个答案:

答案 0 :(得分:5)

您应该使用re.sub

string = re.sub(r"TABLESPACE \"D[0-9]+\"", "TABLESPACE COPY_TBS", string)

string.replace不适用于正则表达式。

Demo

此外,我更改了模式以将表空间名称匹配为D[0-9]+,假设您想要D,后跟任意数字。如果您确实只想限制两位数,请使用D[0-9]{2}

答案 1 :(得分:1)

与其他答案不同,有一种更简单的方法可以做到。将数据分配给变量:

$.ajax({
   type: "POST",
   url: 'uploadFileProduction',
   data: JSON.stringify(data),
   contentType: "application/json; charset=utf-8" // <- this is what you should add
});

然后使用循环替换:

a = '''CREATE TABLE "USER1"."TEST_TAB"
   (    "O_ID" CHAR(15),
        "CREATED_DATE" DATE DEFAULT SYSDATE,
        "EVENT_ID" NUMBER,
        "LOD" BLOB,
        "TOPIC" VARCHAR2(765),
        "SCHEMA_ID" VARCHAR2(36),
        "RECORD_ID" CHAR(15),
         CONSTRAINT "R_PKCONDUIT_MESSAGE" PRIMARY KEY ("O_ID", "TOPIC", "EVENT_ID", "CREATED_DATE") ENABLE
   ) O INDEX COMPRESS 2 PCTFREE 10 INITRANS 2 MAXTRANS 255  NOLOGGING
  TABLESPACE "D01"
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
 PCTTHRESHOLD 50
 LOB ("LOD") STORE AS BASICFILE (
  DISABLE STORAGE IN ROW CHUNK 8192 RETENTION
  NOCACHE LOGGING
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT))
  PARTITION BY HASH ("O_ID")
 (PARTITION "CONDUIT_MESSAGE01"
 LOB ("LOD") STORE AS BASICFILE "LOD01"(
  TABLESPACE "D01" )
  TABLESPACE "D01" ,
 PARTITION "CONDUIT_MESSAGE02"
 LOB ("LOD") STORE AS BASICFILE "LOD02"(
  TABLESPACE "D02" )
  TABLESPACE "D02" ,
 PARTITION "CONDUIT_MESSAGE03"
 LOB ("LOD") STORE AS BASICFILE "LOD03"(
  TABLESPACE "D03" )
  TABLESPACE "D03" ,
 PARTITION "CONDUIT_MESSAGE04"
 LOB ("LOD") STORE AS BASICFILE "LOD04"(
  TABLESPACE "D04" )
  TABLESPACE "D04" ,
 PARTITION "CONDUIT_MESSAGE05"
 LOB ("LOD") STORE AS BASICFILE "LOD05"(
  TABLESPACE "D05" )
  TABLESPACE "D05" ,
 PARTITION "CONDUIT_MESSAGE06"
 LOB ("LOD") STORE AS BASICFILE "LOD06"(
  TABLESPACE "D06" )
  TABLESPACE "D06"'''

您可以在这里在线进行测试: https://repl.it/@blackskull12/DisfiguredMemorableStructs

编辑:

由于它不适用于大于for i in range(1,7): a = a.replace('TABLESPACE "D0'+str(i)+'"','TABLESPACE COPY_TBS') 的数字,因此您的循环可以像(它可以处理小于100的数字):

10