首页 > 解决方案 > 如何提取每个单词的第一个字母并保留Google Sheets中原始文本的间距、标点、大写和换行符

问题描述

如果我将以下文本放在单元格中:

I'm a little teapot, short and stout. 
Here is my handle, here is my spout. 
When I get all steamed up - hear me shout! 
Tip me over and pour me out. 

我希望另一个单元格中的输出为:

I' a l t, s a s. 
H i m h, h i m s. 
W I g a s u - h m s!
T m o a p m o.

输入中永远不会有数字。

标签: regexgoogle-sheets

解决方案


以下正则表达式模式似乎有效:

\b(\S)[^'\s]*('?)\S*\b

然后只需替换为$1$2. 在此处查看演示。这是您可以使用的 Google 表格代码:

REGEXREPLACE("your text here", "\b(\S)[^'\s]*('?)\S*\b", "$1$2")

正则表达式模式的解释:

\b       word boundary on the left
(\S)     match and capture the first letter of each word
[^'\s]*  then consume any non whitespace content which is also not apostrophe
('?)     match and capture an apostrophe, if present
\S*      consume the remainder of the word
\b       word boundary on the right

推荐阅读