Google Sheets:提取两个字符之间的文本
我有一个字段需要提取两个字符之间的文本。
我找到了函数 REGEXEXTRACT
并且只有当有一个字符时我才能让它工作。但我无法让它与多个角色一起使用。
示例
2020-02: Test Course (QAS)
我需要在 :
之后和 (
之前提取文本。
所以它只会返回
Test Course
I have a field where I need to extract the text between two characters.
I've found the function REGEXEXTRACT
and I got it to work, only when there is one character. But I can't get it to work with multiple characters.
Example
2020-02: Test Course (QAS)
I need to extract text after :
and before (
.
So it would just return
Test Course
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用 INDEX() 和 SPLIT() 函数,如下所示:
Split 使用 : 和 ( 将文本分成 3 部分,然后 INDEX 选择第二部分。
TRIM() 只是去掉空格。
One way to do that would be with the INDEX() and SPLIT() functions like this:
Split splits the text into 3 parts using the : and (, then INDEX chooses the second part.
The TRIM() just gets rid of the spaces.
如果仅适用于一个单元格(例如 A2):
=IFERROR(TRIM(REGEXEXTRACT(A2,":([^\(]+)")))
这将返回您想要的内容,无论空格如何如果未找到匹配项,则在冒号之后或左括号之前返回 null
如果要处理整个范围(例如 A2:A),请将以下内容放入其他空 Col 的 B2 中。 B:
=ArrayFormula(IF(A2:A="",,IFERROR(TRIM(REGEXEXTRACT(A2:A,":([^\(]+)")),A2:A)))
这将返回您想要的内容,无论冒号后面或左括号之前是否有空格。如果未找到匹配项,则将返回原始字符串,
在这两种情况下,都会返回 REGEX 字符串...
:([^\(]+)
... 表示“不是左括号且在冒号后面的任意数量的字符的分组”。
If it's for just one cell (say A2):
=IFERROR(TRIM(REGEXEXTRACT(A2,":([^\(]+)")))
This will return what you want regardless of spaces after the colon or before the opening parenthesis. If no match is found, null will be returned.
If it's to process an entire range (say, A2:A), place the following in, say, B2 of an otherwise empty Col B:
=ArrayFormula(IF(A2:A="",,IFERROR(TRIM(REGEXEXTRACT(A2:A,":([^\(]+)")),A2:A)))
This will return what you want regardless of spaces after the colon or before the opening parenthesis. If no match is found, the original string will be returned.
In both cases, the REGEX string...
:([^\(]+)
... means "a grouping of any number of characters that aren't an opening parenthesis and which follows a colon."