【发布时间】: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