regex - Google Sheets - 将 split 与 regexextract/regexreplace 相结合

标签 regex google-sheets

我正在处理一本正确设置一列的工作簿。本质上,它的设置使得该值:

This is Foo, and I hate it
This is Bar, and I like it
And this is Baz

显示为以逗号分隔,例如:

This is Foo, and I hate it,This is Bar, and I like it,And this is Baz

我需要找到一种方法来分割这些值。我首先想到使用带有逗号作为分隔符的 Split,但它不起作用,因为某些单独的值中包含逗号。

我正在考虑使用正则表达式来捕获逗号后跟大写字母(例如,[A-Z])并将其与 split 函数结合起来。这可能吗?基本上是使用逗号和大写字母作为分隔符来分割单元格。

尝试使用 Regexreplace 将逗号后跟的大写字母替换为非逗号的分隔符:

=REGEXREPLACE(A2, ",[A-Z]", ";")

这会导致类似的结果

这是 Foo,我讨厌它;他是 Bar,我喜欢它;这是 Baz

然后我可以使用分号分割它们,但随后我会丢失其他值的第一个字符。

最佳答案

如果您反过来使用 SUBSTITUTE?,您可以用分号更改逗号和空格,然后用逗号分隔:

=SPLIT(SUBSTITUTE(A1,", ","; "),",")

enter image description here

如果您想恢复逗号,请使用 ARRAYFORMULA 和 SUBSTITUTE 包装该公式:

=ARRAYFORMULA(SUBSTITUTE(SPLIT(SUBSTITUTE(A1,", ","; "),","),";",","))

enter image description here

关于regex - Google Sheets - 将 split 与 regexextract/regexreplace 相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76582009/

相关文章:

python - 基于 url 的正则表达式前瞻

google-sheets - 弹性使用谷歌表格中的多个过滤器功能

google-sheets - 在 Google 表格中粘贴 dd/mm/yyyy 日期

python - 从谷歌表格中执行 python 脚本

google-apps-script - Google Apps 脚本 : Dynamically creating spreadsheet menu items

javascript - 正则表达式 : get word without word before it

C# 正则表达式忽略尾随字符

python - 使用 Python 3.5 从 CSV 文件的两列中删除特定字符

python - 关于正则表达式和 XML

php - google sheets 将数据附加到 sheet api 返回 401 身份验证错误