Regex 如何提取每个单词的第一个字母,并在谷歌表单中保留原始文本的间距、标点符号、大写和换行符
如果我将以下文本放入单元格:Regex 如何提取每个单词的第一个字母,并在谷歌表单中保留原始文本的间距、标点符号、大写和换行符,regex,google-sheets,Regex,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
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.
输入中永远不会有数字。以下正则表达式模式似乎正在工作:
\b(\S)[^'\S]*('?)\S*\b
然后换成$1$2
。以下是您可以使用的Google Sheets代码:
REGEXREPLACE("your text here", "\b(\S)[^'\s]*('?)\S*\b", "$1$2")
正则表达式模式的解释:
\b左边的单词边界
(\S)匹配并捕获每个单词的第一个字母
[^'\s]*然后使用任何也不是撇号的非空白内容
(“?)匹配并捕获撇号(如果存在)
\使用单词的剩余部分
\右边的单词边界
非常好。非常感谢。