REGEXEXTRACT与捕获组 [英] REGEXEXTRACT with capturing group
问题描述
我想知道在Google表格中使用 REGEXEXTRACT()
时是否可以引用在同一表达式中捕获的组?
I wonder if there is a way to refer to the group captured in same expression when using REGEXEXTRACT()
in Google Sheets?
假设我们有一个示例字符串: aaa123bbb123ccc456ddd123eee123fff456ggg
Let's say we have a sample string:
aaa123bbb123ccc456ddd123eee123fff456ggg
,我们想提取出至少3次出现3位数字的部分.通常我会这样使用正则表达式:(\ d {3})(?:[^ \ 1] * \ 1){2,}
and we'd like to extract the part where some 3 digits occure at least 3 times.
Normally I would use regex like this:
(\d{3})(?:[^\1]*\1){2,}
但是如何指代第一组 = REGEXEXTRACT(A1;(\ d {3})(?:[^ \ 1] * \ 1){2,}")
?这会在表格中返回错误.
but how to refer to the first group in
=REGEXEXTRACT(A1;"(\d{3})(?:[^\1]*\1){2,}")
?
This one returns error in Sheets.
推荐答案
RE2模式中不提供反向引用支持,您需要编写一个自定义JS函数以获取所需的内容:
There is no backreference support in RE2 patterns, you need to write a custom JS function to get what you need:
function IS_THREE_DIGIT_REPEATING(input) {
var rx = /(\d{3})(.*\1){2}/;
var res = rx.exec(input);
return res ? res[1] : "No";
}
它将打印单元格中第一个捕获组的内容(重复的3位数字),如果不匹配,则打印否
.
It will print the contents of the first capturing group in the cell (the 3 digits that are repeating) or No
if there is no match.
模式详细信息
-
(\ d {3})
-捕获组1:三位数 -
(.* \ 1){2}
-连续出现2个除换行符以外的0+个字符,其后的值与第1组中捕获的值相同.
(\d{3})
- Capturing group 1: three digits(.*\1){2}
- 2 consecutive occurrences of any 0+ chars other than linebreak chars followed with the same value as captured in Group 1.
这篇关于REGEXEXTRACT与捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!