zmv - do not change case if there are more than one consecutive uppercase letters

可紊 提交于 2021-02-08 03:00:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!