如何剪切(1) 个驼峰字?
Bash 中有没有一种简单的方法可以将驼峰式单词拆分为其组成词?
例如,我想将 aCertainCamelCasedWord 拆分为“a certain Camel Cased Word”,并能够选择我感兴趣的那些字段。 当单词分隔符是下划线时,可以使用 cut(1) 轻松完成此操作,但是当单词是驼峰式时我该如何做到这一点?
Is there an easy way in Bash to split a camelcased word into its constituent words?
For example, I want to split aCertainCamelCasedWord into 'a Certain Camel Cased Word' and be able to select those fields that interest me. This is trivially done with cut(1) when the word separator is the underscore, but how can I do this when the word is camelcased?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sed 's/\([AZ]\)/ \1/g'
捕获每个大写字母,并用捕获的整个流替换前导空格。
sed 's/\([A-Z]\)/ \1/g'
Captures each capital letter and substitutes a leading space with the capture for the whole stream.
如果您不需要拆分全部大写的单词,则此解决方案有效。 例如,使用您将得到的最佳答案:
但是使用我的解决方案,您将得到:
This solution works if you need to not split up words that are all caps. For example, using the top answer you'll get:
But instead with my solution, you'll get:
此答案在存在第二个实例时无法正常工作多个大写
So 且需要附加表达式
This answer does not work correctly when there is a second instance of multiple uppercase
So and additional expression is required for that
纯 Bash:
Pure Bash: