英文:
RegEx: match multi-line block by 1st and last lines
问题
我需要删除所有包含美元符号("$")的表名的CREATE TABLE块,使用Python的re
库。
PL/SQL脚本相对格式良好。特别是,第一行将始终以"CREATE TABLE"开头,并且还将包含表名(即,在"CREATE TABLE"和表名之间没有换行符);整个"CREATE TABLE ..."语句的闭合圆括号将始终位于单独的一行,并且它将始终是该行中唯一的字符。
示例:
CREATE TABLE "SCH"."SOME$TABLE"
( "NAME" VARCHAR2(100 CHAR),
"DATA1" BLOB,
"TIMESTAMP1" DATE
)
我正在寻找与Python的re
库兼容的正则表达式,可以匹配上述的5行。
我尝试了以下内容:
(?m)^CREATE TABLE.*$.*[\s\S]^\)$
但它没有匹配任何内容。
我做错了什么?
英文:
There is a large block of text (a PL/SQL DDL script).
I need to remove all CREATE TABLE blocks where table name contains a dollar sign ("$"), using Python re
library.
The PL/SQL script is relatively well formatted. In particular, the first line will always start with CREATE TABLE
and will also contain the name of the table (i.,e. there is no line break between CREATE TABLE and the table name); the closing round bracket of the entire CREATE TABLE ...
statement will always be the on a separate line and it will always be the only character in the line.
Example:
CREATE TABLE "SCH"."SOME$TABLE"
( "NAME" VARCHAR2(100 CHAR),
"DATA1" BLOB,
"TIMESTAMP1" DATE
)
I am looking for a regular expression compatible with Python's re
library that would match the above 5 lines.
I tried the following:
(?m)^CREATE TABLE.*$.*[\s\S]^\)$
But it is not matching anything.
What am I doing wrong?
答案1
得分: 1
I believe your error is with the [\s\S]
character group, as it is only matching a single character
You could append a +?
quantifier, to match up to the ^\)$
.
(?m)^CREATE TABLE.*$.*[\s\S]+?^\)$
英文:
> "... What am I doing wrong?"
I believe your error is with the [\s\S]
character group, as it is only matching a single character
You could append a +?
quantifier, to match up to the ^\)$
.
(?m)^CREATE TABLE.*$.*[\s\S]+?^\)$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论