首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >需要使用Google Sheet中的REGEXEXTRACT()在字符串之间提取字符串

需要使用Google Sheet中的REGEXEXTRACT()在字符串之间提取字符串
EN

Stack Overflow用户
提问于 2021-11-17 06:27:50
回答 2查看 1.2K关注 0票数 2

我有一条类似于这样的字符串:"abcd abcd | abcde | Degree SP | xyz abcd | abcd ABC"

我需要使用正则表达式提取"Degree SP"。我怎么能这么做?这里的条件是:

  • 字符串以"SP“结尾
  • 字符串在最后一个“\”之后开始。

我正在尝试Google公式REGEXEXTRACT(<input string>, "[\|\s].+SR[\s\|]"),它返回" | abcde | Degree SP "。如何才能限制从上一次“\”中提取

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-17 10:11:25

如果字符串Degree SP应该位于管道和空格之间:

代码语言:javascript
运行
复制
\|\s([^\s|][^|]*SP)\s\|
  • \|\s匹配|和空格字符
  • ( Capture group 1
    • [^\s|]与空格或|以外的单个字符匹配。
    • [^|]*SP匹配可选字符( |和匹配SP除外)

  • )闭组1
  • \s\|匹配空格char和|

Regex演示

如果只有Degree SP之后的管道是强制性的:

代码语言:javascript
运行
复制
([^\s|][^|]*SP)\s*\|

Regex演示

票数 2
EN

Stack Overflow用户

发布于 2021-11-17 06:41:04

使用您所显示的示例,请尝试使用以下正则表达式。

代码语言:javascript
运行
复制
^.*?\s+\S+\s+\|\s+\S+\s+\|\s+([^\\|]*)\s+\|.*$

上述regex在线演示

,您希望在|的第二次和第三次出现之间捕获值,该值以SP字符串结尾,然后尝试以下正则表达式:

代码语言:javascript
运行
复制
^.*?\s+\S+\s+\|\s+\S+\s+\|\s+([^\\|]*SP)\s+\|.*$

上述regex在线演示

解释:添加了上面的详细说明。

代码语言:javascript
运行
复制
^.*?\s+\S+\s+  ##Matching from starting of value with a lazy match till 1st occurrence of spaces followed by 1 or more non-spaces followed by 1 or more spaces.
\|\s+\S+\s+\|  ##Matching |(literal) followed by spaces followed by 1 or more non-spaces followed by spaces with |(literal character) here.
\s+            ##Matching 1 or more spaces occurrences here.
([^\\|]*)      ##Creating 1 and only capturing group which has everything till next occurrence of | to get Degree SP value mentioned by OP in samples.
\s+\|.*$       ##Matching 1 or spaces followed by | till last of value/line.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69999794

复制
相关文章

相似问题

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