问题
I am using the following command
zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]])/-$match[1]}#-}'
to transform
% tree
.
├── EmptyFile.txt
├── FirstDirectoryName
│ ├── FourthDirectoryName
│ ├── secondDirectoryName
│ └── thirdDirectoryName
├── FourthDirectoryName
├── secondDirectoryName
└── thirdDirectoryName
To
% tree
.
├── EmptyFile.txt
├── first-directory-name
│ ├── fourth-directory-name
│ ├── second-directory-name
│ └── third-directory-name
├── fourth-directory-name
├── second-directory-name
└── third-directory-name
However there is a small problem.
I do not want to lowercase if there are more than one consecutive uppercase letters. I just want to put -
behind the capital letters given it is not in the beginning.
% tree
.
├── DDDDDDD
├── FirstFolderToRename
│ ├── DDDDDDD
│ └── ThisIsDDDDD
├── secondFolderToRename
│ ├── DDDDDDD
│ └── ThisIsDDDDD
└── ThisIsDDDDD
Current Output
% 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
Expected Output
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
回答1:
Specify one or more upper case characters
. That is [[:upper:]]##
with zsh's extended globbing (which zmv uses). ##
is similar to regex +
quantifier. A single #
is zero or more
, similar to regex *
.
zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]]##)/-$match[1]}#-}'
Optionally, convert leading upper case characters separately instead of removing initial -
afterwards:
${1}${(LM)2##[[:upper:]]#}${(L)${2##[[:upper:]]#}//(#b)([[:upper:]]##)/-$match[1]}
回答2:
From 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}}'
}
来源:https://stackoverflow.com/questions/62850317/zmv-do-not-change-case-if-there-are-more-than-one-consecutive-uppercase-letter