我有一个脚本采用mythtv录制的节目,并使用手刹在h264编码。该脚本是用Perl编写的。
我的问题是如何使用perl替换空格和特殊字符和下划线?
输出"Parks and Recreation - S05E01 - Ms. Knope Goes to Washington"
我希望它看起来像这样
Parks_and_Recreation_S05E01_Ms__Knope_Goes_to_Washington
提前致谢。我确实做了一些谷歌搜索,但发现了我可以实现的任何有用的东西。
答案 0 :(得分:8)
这样的事情可能会这样做 - 请注意,如果你改变这样的字符串,你可能会引入重复。
my $input ="Parks and Recreation - S05E01 - Ms. Knope Goes to Washington";
$input =~ s/ - /_/g; # Replace all " - " with "_"
$input =~ s/[^A-Za-z0-9]/_/g; # Replace all non-alphanumericals with "_"
print $input;
输出:
Parks_and_Recreation_S05E01_Ms__Knope_Goes_to_Washington
修改强>
下面的Érics评论非常相关,这是一个稍微好一点的方法,在替换之前用非重音替换重音字符:
use utf8;
use Unicode::Normalize;
my $input="La femme d'à côté";
my $result = NFD($input); # Unicode normalization Form D (NFD), canonical decomposition.
$result !~ s/[^[:ascii:]]//g; # Remove all non-ascii.
$result =~ s/ - /_/g; # Replace all " - " with "_"
$result =~ s/[^A-Za-z0-9]/_/g; # Replace all non-alphanumericals with _
print $result;
此变体输出:
La_femme_d_a_cote
答案 1 :(得分:2)
my $input = "Parks and Recreation - S05E01 - Ms. Knope Goes to Washington";
$input =~ s/\W/_/g; # Replace anything other than letters, numbers and underscore
输出:
Parks_and_Recreation___S05E01___Ms__Knope_Goes_to_Washington
答案 2 :(得分:0)
perl -pe 's/[^A-Za-z0-9]/_/g'
测试:
> echo "Parks and Recreation - S05E01 - Ms. Knope Goes to Washington"|perl -pe 's/[^A-Za-z0-9]/_/g'
Parks_and_Recreation___S05E01___Ms__Knope_Goes_to_Washington