RegEx包围包含字符串“; @tab; @tab; @tab;”的文本带引号

问题描述

我正在尝试创建一个 PCRE RegEx ,该环绕字符串,其中包含带引号(;@tab;@tab;@tab;)的字符串"。输入内容由多行文字组成,其中包含1列或多列,并用标签分隔。必须用引号("括起来的字符串可以在行的开头,行的结尾或行的中间。一行可以包含必须包围的 0个或更多字符串。@H_502_15@ 示例:

1   some text with spaces
1   some text with spaces   anotherValue
1   some text with spaces   anotherValue    3452.2
val_so  some text with spaces   anotherValue    3452.2
val_so space    some text with spaces   anotherValue    3452.2
some text with spaces   anotherValue    3   other text with spaces

1   some;t@b;t@b;t@b;text;t@b;t@b;t@b;with tabs
1   some;t@b;t@b;t@b;text;t@b;t@b;t@b;with tabs and spaces  anotherValue
1   some;t@b;t@b;t@b;text;t@b;t@b;t@b;with spaces   anotherValue    3452.2
val_so  some;t@b;t@b;t@b;text with spaces and;t@b;t@b;t@b; tabs anotherValue    3452.2
val_so space    some text with;t@b;t@b;t@b; spaces amd tabs anotherValue    3452.2
some text;t@b;t@b;t@b; with spaces  anotherValue    3   other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs
;t@b;t@b;t@b; with spaces   anotherValue    3   other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs
;t@b;t@b;t@b;;t@b;t@b;t@b; with spaces  anotherValue    3   other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs

必须成为

1   some text with spaces
1   some text with spaces   anotherValue
1   some text with spaces   anotherValue    3452.2
val_so  some text with spaces   anotherValue    3452.2
val_so space    some text with spaces   anotherValue    3452.2
some text with spaces   anotherValue    3   other text with spaces

1   "some;t@b;t@b;t@b;text;t@b;t@b;t@b;with tabs"
1   "some;t@b;t@b;t@b;text;t@b;t@b;t@b;with tabs and spaces"    anotherValue
1   "some;t@b;t@b;t@b;text;t@b;t@b;t@b;with spaces" anotherValue    3452.2
val_so  "some;t@b;t@b;t@b;text with spaces and;t@b;t@b;t@b; tabs"   anotherValue    3452.2
val_so space    "some text with;t@b;t@b;t@b; spaces amd tabs"   anotherValue    3452.2
"some text;t@b;t@b;t@b; with spaces"    anotherValue    3   "other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs"
";t@b;t@b;t@b; with spaces" anotherValue    3   "other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs"
";t@b;t@b;t@b;;t@b;t@b;t@b; with spaces"    anotherValue    3   "other text;t@b;t@b;t@b;with spaces;t@b;t@b;t@b;and tabs"

我尝试了以下 PCRE RegEx 搜索RegEx \b(([^\t]+);t@b;([^\t]+))\bg标志 replace RegEx :{{ 1}},但是它可以跨多行匹配字符串(匹配不会在行的末尾停止),但是我希望每一行都单独进行匹配。

解决方法

由于数据由制表符分隔,并且您不想跨换行符,因此可以通过在否定的字符类中添加换行符来将它们排除在匹配之外。

您可以省略边界,因为例如它在;的开头和结尾都不会与;t@b;t@b;t@b; 匹配

您不需要捕获组,因为您希望替换双引号之间的整个匹配项。

[^\t\r\n]+;t@b;[^\t\r\n]+

查看regex demo

在替换中,使用双引号之间的整个匹配项。

"$0"