如何剪切(1) 个驼峰字?

发布于 2024-07-14 12:06:29 字数 182 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对岸观火 2024-07-21 12:06:29

sed 's/\([AZ]\)/ \1/g'

捕获每个大写字母,并用捕获的整个流替换前导空格。

$ echo "aCertainCamelCasedWord" | sed 's/\([A-Z]\)/ \1/g'
a Certain Camel Cased Word

sed 's/\([A-Z]\)/ \1/g'

Captures each capital letter and substitutes a leading space with the capture for the whole stream.

$ echo "aCertainCamelCasedWord" | sed 's/\([A-Z]\)/ \1/g'
a Certain Camel Cased Word
风和你 2024-07-21 12:06:29

如果您不需要拆分全部大写的单词,则此解决方案有效。 例如,使用您将得到的最佳答案:

$ echo 'FAQPage' | sed 's/\([A-Z]\)/ \1/g' 
F A Q Page

但是使用我的解决方案,您将得到:

$ echo 'FAQPage' | sed 's/\([A-Z][^A-Z]\)/ \1/g'
FAQ Page

注意:当存在多个大写单词的第二个实例时,此方法无法正常工作,例如:

$ echo 'FAQPageOneReplacedByFAQPageTwo' | sed 's|\([A-Z][^A-Z]\)| \1|g'
FAQ Page One Replaced ByFAQ Page Two

This solution works if you need to not split up words that are all caps. For example, using the top answer you'll get:

$ echo 'FAQPage' | sed 's/\([A-Z]\)/ \1/g' 
F A Q Page

But instead with my solution, you'll get:

$ echo 'FAQPage' | sed 's/\([A-Z][^A-Z]\)/ \1/g'
FAQ Page

Note: This does not work correctly when there is a second instance of multiple uppercase words, for example:

$ echo 'FAQPageOneReplacedByFAQPageTwo' | sed 's|\([A-Z][^A-Z]\)| \1|g'
FAQ Page One Replaced ByFAQ Page Two
谜泪 2024-07-21 12:06:29

此答案在存在第二个实例时无法正常工作多个大写

echo 'FAQPageOneReplacedByFAQPageTwo' | sed 's|\([A-Z][^A-Z]\)| \1|g'
FAQ Page One Replaced ByFAQ Page Two

So 且需要附加表达式

 echo 'FAQPageOneReplacedByFAQPageTwo' | sed -e 's|\([A-Z][^A-Z]\)| \1|g' -e 's|\([a-z]\)\([A-Z]\)|\1 \2|g'
 FAQ Page One Replaced By FAQ Page Two

This answer does not work correctly when there is a second instance of multiple uppercase

echo 'FAQPageOneReplacedByFAQPageTwo' | sed 's|\([A-Z][^A-Z]\)| \1|g'
FAQ Page One Replaced ByFAQ Page Two

So and additional expression is required for that

 echo 'FAQPageOneReplacedByFAQPageTwo' | sed -e 's|\([A-Z][^A-Z]\)| \1|g' -e 's|\([a-z]\)\([A-Z]\)|\1 \2|g'
 FAQ Page One Replaced By FAQ Page Two
无敌元气妹 2024-07-21 12:06:29

纯 Bash:

name="aCertainCamelCasedWord"

declare -a word                                 # the word array

counter1=0                                      # count characters
counter2=0                                      # count words

while [ $counter1 -lt ${#name} ] ; do
  nextchar=${name:${counter1}:1}
  if [[ $nextchar =~ [[:upper:]] ]] ; then
    ((counter2++))
    word[${counter2}]=$nextchar
  else
    word[${counter2}]=${word[${counter2}]}$nextchar
  fi
  ((counter1++))
done

echo -e "'${word[@]}'"

Pure Bash:

name="aCertainCamelCasedWord"

declare -a word                                 # the word array

counter1=0                                      # count characters
counter2=0                                      # count words

while [ $counter1 -lt ${#name} ] ; do
  nextchar=${name:${counter1}:1}
  if [[ $nextchar =~ [[:upper:]] ]] ; then
    ((counter2++))
    word[${counter2}]=$nextchar
  else
    word[${counter2}]=${word[${counter2}]}$nextchar
  fi
  ((counter1++))
done

echo -e "'${word[@]}'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文