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 m o.
输入中永远不会有数字。
解决方案
以下正则表达式模式似乎有效:
\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
推荐阅读
- c# - 在 MVC 中授权时如何防止“向服务器发送请求时发生传输级错误”错误
- sql - PostgreSQL:让 STRING_AGG 尊重 CTE 的排序
- graphql - GraphQL 代码生成 React Hooks + Typescript (graphql-codegen)
- swift - 当键盘出现在“ChildView”上时“ParentView”向上移动 - SwiftUI
- reactjs - 无法对未安装的组件执行 React 状态更新以进行 useCallback
- python - 计数符合某些标准的组合?
- azure-data-explorer - kql 查询大小的部分查询失败
- reactjs - React Draft wysiwyg 自动对焦弹出窗口
- html - 悬停在滚动效果上会导致元素的宽度发生变化
- angularjs - 如何从 Form.IO 表单构建器中删除类别或字段?