Format file names using Regular Expression

Posted on

Question :

I’m trying to rename some files using Regular Expression, but I’m stuck in a specific format. I have video files in this format:

  

Yu.Yu.Hakusho Ep.001 – The Death

I need to format using regex for:

  

Yu.Yu.Hakusho.S01E01 – The Death

I’ve tried using ([^ ]+)*.(w{0,2}).([0-9]+)*.(.*.) to capture only what I need and format, as can be seen below:

$texto = "Yu.Yu.Hakusho Ep.001 - A Morte";
$regex = "/([^ ]+)*.(w{0,2}).([0-9]+)(.*.)/";
preg_match($regex,$texto,$m);   

echo $m[1].".S01E".$m[3].$m[4];

And the output is

  

Yu.Yu.Hakusho.S01E001 – The Death

I’m not sure how to leave the string 001 with only the last two digits.

How do I do this with regex? (I can not use replace, I need to do only with ER myself)

  

Note: I used php as an example, but I need to apply a
  regex, independent of language, as can be seen here in this link:
   link

    

Answer :

I created another regex pattern from scratch, because I found it a bit “disorganized.”

Pattern : (.*?) .*?d?(d{2}).*?(w.*)

Substitution: .S01E -

If you prefer to use the same pattern I was already using, I did a modified version of it: ([^ ]+)* (w{0,2}).d?(d{1,2})*.(.*.) “The substitution remains the same.
    

Leave a Reply

Your email address will not be published. Required fields are marked *