Google 表格 REGEXREPLACE - 根据最后一个字符排除匹配项

问题描述 投票:0回答:1

原文:

Basic medication kit of dentistry Drugs box containing Salbutamol; Glycerol-Trinitrate; Glucagon and Adrenaline

公式:

=TRIM(REGEXREPLACE(G20,"([A-Z][\w\-]*[^;])",CHAR(10)&"· $1"))

实际结果文本:

· Basic medication kit of dentistry 
· Drugs box containing 
· Salbutamol; 
· Glycerol-Trinitrate; 
· Glucagon and 
· Adrenaline

预期结果文本:

· Basic medication kit of dentistry 
· Drugs box containing Salbutamol; Glycerol-Trinitrate; 
· Glucagon and 
· Adrenaline

我想要做的是排除以分号结尾的匹配项。目前,在我的正则表达式公式中,我有

[^;]
,这似乎没有效果。

这可以吗?

regex google-sheets google-sheets-formula
1个回答
0
投票

通过包含非捕获组检查对原始公式进行轻微修改

=trim(regexreplace(A1,"((?:^| )[A-Z][^ ]*[^;](?: |$))",char(10)&"· $1"))

替代方法

=reduce(,split(A1," "),lambda(a,c,trim(a&if(and(regexmatch(left(c),"[A-Z]"),right(c)<>";"),char(10)&"· "," ")&c)))
  • 拆分所有单词,然后检查单词是否以大写字母开头且不以
    ;
  • 结尾,然后一次将它们连接起来

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.