我正在使用以下命令
zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]])/-$match[1]}#-}'
转换
% tree
.
├── EmptyFile.txt
├── FirstDirectoryName
│ ├── FourthDirectoryName
│ ├── secondDirectoryName
│ └── thirdDirectoryName
├── FourthDirectoryName
├── secondDirectoryName
└── thirdDirectoryName
至
% tree
.
├── EmptyFile.txt
├── first-directory-name
│ ├── fourth-directory-name
│ ├── second-directory-name
│ └── third-directory-name
├── fourth-directory-name
├── second-directory-name
└── third-directory-name
然而,有一个小问题。
如果有多个连续的大写字母,我不想小写。我只想把-
放在大写字母后面,因为它不在开头。
% tree
.
├── DDDDDDD
├── FirstFolderToRename
│ ├── DDDDDDD
│ └── ThisIsDDDDD
├── secondFolderToRename
│ ├── DDDDDDD
│ └── ThisIsDDDDD
└── ThisIsDDDDD
电流输出
% zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]])/-$match[1]}#-}'
mv -- FirstFolderToRename/DDDDDDD FirstFolderToRename/d-d-d-d-d-d-d
mv -- FirstFolderToRename/ThisIsDDDDD FirstFolderToRename/this-is-d-d-d-d-d
mv -- secondFolderToRename/DDDDDDD secondFolderToRename/d-d-d-d-d-d-d
mv -- secondFolderToRename/ThisIsDDDDD secondFolderToRename/this-is-d-d-d-d-d
mv -- DDDDDDD d-d-d-d-d-d-d
mv -- FirstFolderToRename first-folder-to-rename
mv -- secondFolderToRename second-folder-to-rename
mv -- ThisIsDDDDD this-is-d-d-d-d-d
预期输出
mv -- FirstFolderToRename/DDDDDDD FirstFolderToRename/DDDDDDD
mv -- FirstFolderToRename/ThisIsDDDDD FirstFolderToRename/this-is-DDDDD
mv -- secondFolderToRename/DDDDDDD secondFolderToRename/DDDDDDD
mv -- secondFolderToRename/ThisIsDDDDD secondFolderToRename/this-is-DDDDD
mv -- DDDDDDD DDDDDDD
mv -- FirstFolderToRename first-folder-to-rename
mv -- secondFolderToRename second-folder-to-rename
mv -- ThisIsDDDDD this-is-DDDDD
指定one or more upper case characters
。这就是具有zsh的扩展globbing(zmv使用(的[[:upper:]]##
。##
类似于正则表达式+
的量词。单个#
是zero or more
,类似于正则表达式*
。
zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]]##)/-$match[1]}#-}'
可选地,单独转换前导大写字符,而不是在之后删除初始-
:
${1}${(LM)2##[[:upper:]]#}${(L)${2##[[:upper:]]#}//(#b)([[:upper:]]##)/-$match[1]}
来自linuxquestions
function CamelOrPascalToKebab() {
zmv -Q '(**/)(*[A-Z]*)(/)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}'
zmv -Q '(**/)(*[A-Z][a-z]*)(/)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}'
}