Google表格公式中的多个正则表达式匹配 [英] Multiple regex matches in Google Sheets formula
问题描述
我试图使用Google Sheets正则表达式公式获取给定字符串中连字符前面所有数字的列表(例如,在单元格 A1
中):
= REGEXEXTRACT(A1,\d-)
我的问题是,它只返回第一个匹配... 我如何获得所有匹配?
示例文本:
A1-Nutrition; A2-ActPhysiq; A2 -BioMeta; A2-病理怨妇; A2-StgMrktg2; H2-Bioth2 / EtudeCas; H2-Bioth2 / Gemmo; H2-Bioth2 /寡; H2-Bioth2 /浦; H2-Bioth2 /有机; H3-Endocrino; H3-Génétiq
我的公式返回 1 -
,而我想得到 1-2-2-2-2-2-2-2-2-2-3-3 -
(作为一个数组或连接文本) 。
我知道我可以使用脚本或其他函数(例如 SPLIT
)来获得所需的结果,但我真正想知道的是如何获得一个re2正则表达式来返回这样的多个匹配的 REGEX。*
Google表格公式。
类似于 g lobal - 第一次匹配后不返回选项
我也尝试用 REGEXREPLACE
删除不需要的文本, ,也没有成功(我无法摆脱其他数字不在连字符之前)。
任何帮助表示赞赏!
Thanks:)
编辑
使用更通用的解决方案:
$ b
= regexreplace(A1,(。)?(\ d-)|(。),
试试这个公式:
>
= regexreplace(regexreplace(A1,[^ \-0-9],),(\d-)|( 。),$ 1)
它会像这样处理字符串:
< <-c $ c>A1-Nutrition; A2-ActPhysiq; A2-BioM --- eta; A2-PH3-Généti*** 566 * 9q
输出:
1-2-2-2-3 -
I'm trying to get the list of all digits preceding a hyphen in a given string (let's say in cell A1
), using a Google Sheets regex formula :
=REGEXEXTRACT(A1, "\d-")
My problem is that it only returns the first match... how can I get all matches?
Example text:
"A1-Nutrition;A2-ActPhysiq;A2-BioMeta;A2-Patho-jour;A2-StgMrktg2;H2-Bioth2/EtudeCas;H2-Bioth2/Gemmo;H2-Bioth2/Oligo;H2-Bioth2/Opo;H2-Bioth2/Organo;H3-Endocrino;H3-Génétiq"
My formula returns 1-
, whereas I want to get 1-2-2-2-2-2-2-2-2-2-3-3-
(either as an array or concatenated text).
I know I could use a script or another function (like SPLIT
) to achieve the desired result, but what I really want to know is how I could get a re2 regular expression to return such multiple matches in a "REGEX.*
" Google Sheets formula.
Something like the "global - Don't return after first match" option on regex101.com
I've also tried removing the undesired text with REGEXREPLACE
, with no success either (I couldn't get rid of other digits not preceding a hyphen).
Any help appreciated! Thanks :)
Edit
I came up with more general solution:
=regexreplace(A1,"(.)?(\d-)|(.)","$2")
Try this formula:
=regexreplace(regexreplace(A1,"[^\-0-9]",""),"(\d-)|(.)","$1")
It will handle string like this:
"A1-Nutrition;A2-ActPhysiq;A2-BioM---eta;A2-PH3-Généti***566*9q"
with output:
1-2-2-2-3-
这篇关于Google表格公式中的多个正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!