是否可以使用单个 perl 正则表达式将 PascalCase1.wav 重命名为 kebab-case-1.wav? [英] Is it possible to rename PascalCase1.wav to kebab-case-1.wav with a single perl regex?
问题描述
这是我的数据示例:
SomePascalCase.wav
ThingsThat1.wav
Are.wav
Here.wav
这是我正在寻找的结果:
Here is the result I'm looking for:
some-pascal-case.wav
things-that-1.wav
are.wav
here.wav
这是我使用的:
for f in *.wav; do
mv "$f" $(
echo "$f" |
perl -pe 's/([A-Z])([a-z]+)(?=[0-9A-Z])/\L\1\2-/g' |
perl -pe 's/([A-Z])([a-z]+)(?=.wav)/\L\1\2/g'
)
done
是否可以将我使用的两个正则表达式合并为一个?
Is it possible to consolidate the two regular expressions I used into a single one?
推荐答案
在这里您不会真正使用正则表达式替换.您将使用 split
然后 join
与破折号.split
模式是先消极后顾.
You wouldn't really use a regex substitution here. You would use split
and then join
with dashes. The split
pattern is a negative look behind followed by a look ahead.
lc join "-", split /(?<=[a-z])(?=[A-Z])/;
例如
pp split /(?<=[a-z])(?=[A-Z])/, "FooBarBaz"
("Foo", "Bar", "Baz")
根据您分隔数字的规则,您只需添加更多带有环视功能的边界检查.
Depending on your rules for seperating out numbers, you would just add more boundary checks with look arounds.
pp split /(?<=[a-z])(?=[A-Z])|(?<=[A-Za-z])(?=[\d])|(?<=[\d])(?=[A-Za-z])/, "Foo1BarBaz1"
("Foo", 1, "Bar", "Baz", 1)
编辑
要将其合并到您的一个班轮中,您可以这样做:
EDIT
To incorporate this into your one liner, you would do this:
f=FooBarBaz1
echo $( echo "$f" | perl -pe '$_ = lc join "-", split /(?<=[a-z])(?=[A-Z])|(?<=[A-Za-z])(?=[\d])|(?<=[\d])(?=[A-Za-z])/;' )
foo-bar-baz-1
用替换来做只会让它变得更复杂.
Doing it with substitutions will just make it more complicated.
HTH
这篇关于是否可以使用单个 perl 正则表达式将 PascalCase1.wav 重命名为 kebab-case-1.wav?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!