thechucklingatom
thechucklingatom

Reputation: 63

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.

Upvotes: 0

Views: 192

Answers (2)

tjd
tjd

Reputation: 4104

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;

Upvotes: 1

PrgmError
PrgmError

Reputation: 113

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.

Upvotes: 0

Related Questions