正则表达式在Google表格中令人反感 [英] Regex positive lookbehind in Google Sheets
问题描述
我有一个带有交易清单的Google表格.我需要在D列中的"end"一词之后的E列中最多隔离三个单词或数字.正确的正则表达式函数应该是正向查找,但RE2不支持该正则表达式(来源:在正则表达式(?= regex)中使用re2 ).
I have a Google Sheet with a list of transactions. I need to isolate up to three words or numbers in the column E that come after the word "end" in the column D. The proper regex function should be the positive lookbehind, but it is not supported in RE2 (source: Using positive-lookahead (?=regex) with re2).
此公式在GSheets中返回错误:
This formula returns an error in GSheets:
=REGEXEXTRACT(D1;"(?<=end\s)(\w+)")
所以我的结论是,在这种情况下,正则表达式是死胡同.
So my conclusion is that regex is a dead end in this case.
如何在GSheets中获得请求的结果?
How do I obtain the requested result in GSheets?
推荐答案
您可以在正则表达式中使用捕获组,使REGEXEXTRACT
仅返回捕获的部分:
You may use a capturing group in your regex to make REGEXEXTRACT
return just that captured part:
=REGEXEXTRACT(D1;"end\s*(\w+)")
如果您需要在end
之后返回1个,2个或3个空格分隔的单词,请使用
If you need to return 1, 2 or 3 whitespace-separated words after end
, use
=REGEXEXTRACT(D1;"end\s*(\w+(?:\s+\w+){0,2})")
请参见在线演示(Golang regex也使用RE2).
See the online demo (Golang regex also uses RE2).
详细信息
-
end
-end
-
\s*
-0+空格 -
(\w+(?:\s+\w+){0,2})
-捕获组1:-
\w+
-1个以上的字符字符(字母,数字或_
) -
(?:\s+\w+){0,2}
-出现0、1或2次-
\s+
-1个以上空格 -
\w+
-1个以上的字符字符.
end
-end
\s*
- 0+ whitespaces(\w+(?:\s+\w+){0,2})
- Capturing group 1:\w+
- 1+ word chars (letters, digits or_
)(?:\s+\w+){0,2}
- 0, 1 or 2 occurrences of\s+
- 1+ whitespaces\w+
- 1+ word chars.
这篇关于正则表达式在Google表格中令人反感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
-