简体   繁体   English

正则表达式 Google 表格有条件

[英]REGEX Google Sheets Conditional

I have this formula in Google sheets.我在 Google 表格中有这个公式。

=REGEXREPLACE(A3, "BOX OF\s*(\d+)?\s*BOTTLE", "BX-$1BT")

Original                          Expected
BOX OF 3 BOTTLE @ 10 TABLET       BX-3BT @ 10 TABLET
BOX OF BOTTLE @ 10 TABLET         BX-BT @ 10 TABLET
BOX OF 2 BLISTER @ 14 TABLET      BX-2BL @ 14 TABLET
BOX OF VIAL 5 ML                  BX-VL 5ML

I would like the formula to work for BOTTLE, BLISTER, etc...我希望该配方适用于 BOTTLE、BLISTER 等...

Is that feasable?这可行吗?

To get those replacement, one option is to use an alternation with capturing groups for the letters of the words and use the capturing groups in the replacement.要获得这些替换,一种选择是对单词的字母使用捕获组的替换,并在替换中使用捕获组。

BOX OF\s*(\d*)\s*(?:(B)O(T)TLE|(BL)ISTER|(V)IA(L))

Regex demo正则表达式演示

=REGEXREPLACE(A3, "BOX OF\s*(\d*)\s*(?:(B)O(T)TLE|(BL)ISTER|(V)IA(L))", "BX-$1$2$3$4$5$6")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM