简体   繁体   中英

Substitute multiple substrings in Perl

I am trying to match Strings that look like this in 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:

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

In a Perl regex, the g modifier continually applies the RegEx until it stops matching.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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