【问题标题】:RegEx to surround text that contains the string ";@tab;@tab;@tab;" with quotes正则表达式包围包含字符串“;@tab;@tab;@tab;”的文本带引号
【发布时间】:2020-10-22 21:04:21
【问题描述】:

我正在尝试创建一个 PCRE RegEx,它环绕 包含带有引号的字符串 ;@tab;@tab;@tab; (") 的字符串。输入由多行文本组成,其中包含 1 列或多列,以 制表符 分隔。必须用引号括起来的字符串 (") 可以位于行首、行尾或行中间。一行可以包含 0 个或多个字符串,这些字符串必须被包围。
示例:

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]+))\b with g flag replace RegEx:"\2" 但它匹配跨多行的字符串(匹配不会在行尾停止),但我希望每一行都单独匹配。

【问题讨论】:

  • 我们可以在编辑问题时看到标签,请将标签保留在示例数据中。
  • 您可以省略捕获组,并省略匹配字符类中的换行符[^\t\n]+;t@b;[^\t\n]+regex101.com/r/SOh0CR/1 在替换中使用双引号之间的完全匹配。
  • 只需使用regex101.com/r/Mpo5mY/1、[^\t\n]*t@b;t@b;t@b[^\t\n]* 并替换为"$0"。

标签: regex pcre regexp-replace


【解决方案1】:

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

您可以省略单词边界,例如,它将与 ; 开头和结尾的 ;t@b;t@b;t@b; 不匹配

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

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

查看regex demo

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

"$0"

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多