简体   繁体   English

在Perl中替换多个子字符串

[英]Substitute multiple substrings in Perl

I am trying to match Strings that look like this in Perl 我正在尝试匹配Perl中看起来像这样的字符串

%TRMMHDT128F422F115<SEP>SOJEZBM12A6D4FEA96<SEP>Thursday<SEP>A Hole In The World (Album Version) [etc]

The strings will not always have the parentheses and/or brackets at the end. 字符串的结尾不一定总是带有括号和/或括号。 What I want to do is remove all the fluff around the song, and eventually all the punctuation in the song. 我想做的是去除歌曲周围的所有绒毛,最后去除歌曲中的所有标点符号。 I can do this currently in two passes with these statements: 我现在可以使用以下语句通过两步来完成此操作:

$line =~ s/.*>//;
$line =~ s/(\(.*)|(\[.*)//;

I would like to do this all at once, but if I add a pipe | 我想一次完成所有操作,但是如果添加管道| after the first expression and before the second it will not remove anything in the parentheses or brackets. 在第一个表达式之后和第二个表达式之前,它将不会删除括号或方括号中的任何内容。 Like so: 像这样:

$line =~ s/.*>|(\(.*)|(\[.*)//;

Now in a regex tester this matches everything I would like it to match but it isn't substituting everything. 现在,在正则表达式测试器中,它可以匹配我希望匹配的所有内容,但不能代替所有内容。

Substitute multiple substrings in Perl: 在Perl中替换多个子字符串:

$line =~ s/.*>|(\(.*)|(\[.*)//g;

In a Perl regex, the g modifier continually applies the RegEx until it stops matching. 在Perl正则表达式中, g修饰符会不断应用RegEx直到停止匹配。

Though as the last two conditions are nearly identical, I'd probably consolidate it to: 尽管由于后两个条件几乎相同,所以我可能会将其合并为:

$line =~ s/.*>|([([].*)//g;

Try this: 尝试这个:

$line =~ s/(.*>)(.*)(\(.*)/$2/;

What this does is that it matches the entire line for that pattern, and substitutes the entire line with $2 which is the song title. 这是因为它匹配该模式的整行,并用歌曲标题$ 2替换整行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM