正则表达式:通过第一行和最后一行匹配多行块

huangapple go评论60阅读模式
英文:

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]+?^\)$

huangapple
  • 本文由 发表于 2023年7月3日 19:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定