首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用引号环绕包含字符串";@tab;@tab;@tab;“RegEx的文本

用引号环绕包含字符串";@tab;@tab;@tab;“RegEx的文本
EN

Stack Overflow用户
提问于 2020-10-22 21:04:21
回答 1查看 159关注 0票数 0

我正在尝试创建一个PCRE RegEx包围包含带有引号(")的字符串;@tab;@tab;@tab;字符串。输入由多行文本组成,其中包含用选项卡分隔的一个或多个列。必须用引号(")包围的字符串可以位于行的乞求处、行的末尾或行的中间。行可以包含必须被包围的0或更多字符串

示例:

代码语言:javascript
运行
复制
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

必须成为

代码语言:javascript
运行
复制
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:带有g标志的\b(([^\t]+);t@b;([^\t]+))\b替换RegEx:"\2",但是它匹配跨多行的字符串(匹配不停止在行的末尾),但我希望每一行都分别匹配。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-22 21:46:24

由于数据是由制表符分隔的,而且您不想跨换行符,所以可以通过在否定式字符类中添加换行符来排除它们的匹配。

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

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

代码语言:javascript
运行
复制
[^\t\r\n]+;t@b;[^\t\r\n]+

regex演示

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

代码语言:javascript
运行
复制
"$0"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64490533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档