gpt4 book ai didi

regex - 通过 Perl 更改文件名

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:06 25 4
gpt4 key购买 nike

如何更改文件名,如下所示:SomeCodeFile.extension转换为 C/C++ 表示法:some_code_file.extension 。我想使用 Perl + Find + Regex 来打印这些名称。

最佳答案

您可以使用正则表达式:

perl -e 'for $f (@ARGV) {
($n = $f) =~ s/(?<=.)([[:upper:]])/_\l$1/g;
rename $f, lcfirst $n;
}' SomeCodeFiles*
  • [[:upper:]]匹配大写字母。
  • (?<=.)是断言后面的外观,即字母前面有一些东西(我们不想要 _some_code_file.ext )
  • \l将以下字母更改为小写(类似于 lcfirst )

请注意,在某些情况下结果可能会令人不快( x_m_l2_j_s_o_n.cu_r_l__validator.cpp )。

关于regex - 通过 Perl 更改文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com