Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

perl regex help

3 Inches Of Blood - Fire Up The Blades (2007) [v1]
Abuse - A Sunday Morning Killing Spree (1999) [192]
Agressor - 01. Neverending Destiny (1990) [ogg224]


I need a perl regex to replace any character sequence or digit inside the [] at the end of the line.

I'm trying to learn perl regex but struggling to do anything other than really basic stuff.
The result of the regex should do something like below

3 Inches Of Blood - Fire Up The Blades (2007) [v0]
Abuse - A Sunday Morning Killing Spree (1999) [v0]
Agressor - 01. Neverending Destiny (1990) [v0]


It must only replace at the end of the line too. There could be examples of square brackets in the title. And the replace variable can be any length and must replace whatever is in the [ ] ...not just the same length as the replacing variable.

Thanks in advance
0
NorthReptile
Asked:
NorthReptile
  • 2
  • 2
1 Solution
 
ozoCommented:
s/\[.*?\]$/v0/gm;
0
 
NorthReptileAuthor Commented:
Thanks a lot.

What does the /gm do at the end?

One further question, if you don't mind.

In same cases if the word ogg is in the  [ ] followed by a number could i exclude this in the same regex?

i.e

blah blah [ogg256]
blah again [ogg224]

both would be untouched by the replacing.
0
 
NorthReptileAuthor Commented:
I'm trying to break down what you did to understand it further.

 s/\[.*\]$/\[$v\]/gm;

This also works with out [.*?]. Can you tell me what the difference is?

In one of the guides on regex it has the same definition for * and ?

*      # Zero or more of the last character
?      # Zero or one of the last character

but

 s/\[.?\]$/\[$v\]/gm;

does not work.
0
 
ozoCommented:
\[.*\] matches the longest sequence
\[.*?\] matches the shortest sequence
but since they both must match at the end $
it doesn't make a difference in this case, and I should have said
s/\[[^][]*\]$/v0/gm;
so that it only matches the last [] when there are more than one
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now