如何使用Google表中的Regexreplace仅用仪表板替换字符串中的第一个Whitespace?
试图替换: “ abcd efgh ijkl”
“ abcd-efgh ijkl”
在Google表中。 我尝试了:
=REGEXREPLACE(C5,"(?:[A-Za-z]+)(\s)","\-$1")
但是它不起作用。我认为我需要使用乞讨/结束锚(^$),但这也失败了。 有什么想法吗?
Trying to replace:"abcd efgh ijkl"
with "abcd-efgh ijkl"
in google sheets.
I tried:
=REGEXREPLACE(C5,"(?:[A-Za-z]+)(\s)","\-$1")
but it doesn't work. I think I'd need to use beg/end anchors (^$)but this failed too.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
捕获领先的部分并将其放回替换中:
这捕获了第一个空格的开始之间的所有内容,消耗了空格,并用捕获的内容和破折号替换 - 用仪表板有效地代替了空间。
正则的重要部分是
?
。*
,它使。尽可能少。与普通的
。*
相反,它尽可能匹配 (即greedy )。Capture the leading part and put it back in the replacement:
This captures everything between the start to the first whitespace, consuming the whitespace, and replaces it with the captured content and a dash - effectively replacing the space with a dash.
The important part of the regex is the
?
after.*
, which makes the.*
reluctant, which matches as little as possible. As opposed to plain.*
, which matches as much as possible (ie greedy).