Format file names using Regular Expression

Asked

Viewed 790 times

1

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

Yu.Yu.Hakusho Ep.001 - Death

I need to format using regex to:

Yu.Yu.Hakusho.S01E01 - Death

I 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 way out is

Yu.Yu.Hakusho.S01E001 - Death

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

How do I do this substitution with regex? (I can’t use replace, I need to do it with ER only)

Note: I used php as an example, but I need to apply one regex, language independent, as can be seen here at this link: https://regex101.com/r/dC3aN4/1

1 answer

2


I created another Pattern regex from scratch, because I found yours a little "disorganized."

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

Substituting: \1.S01E\2 - \3

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

  • If you’re not going to abuse your goodwill, could you explain how this regex works? I have other standards to apply, maybe understanding this filter I can just adapt it to other situations :)

  • 1

    If you really want to understand how regex works I recommend looking for a book or course online. (This is the first result on Google and looks good: http://learn.vidageek.net/learnregex) But explaining the strategy of regex that I did there above, he does the following: Get everything up to a space and put it in 1, get everything up to any number, get two numbers and put in 2, get everything up to some letter, get from the letter to the end of the string and put in 3.

  • Being that in the part that he "puts two numbers in the 2" can or not have a number before, because of the ? who is after the \d.

  • I get it now, I just wasn’t quite getting the hang of this ?. I’ll use both for reference, thank you :)

  • Well, I think this example will only work until episode 99, when it gets to 101 it will save as S01E01

  • Slightly changing the Pattern made by our friend @Danieldutra to one that works until the 999° episode : /(.?) .?Ep.? ( d{2,3}).?(\w.)/

  • Actually, my solution will only work properly until episode 99, but according to the question, it only wants to display two numbers after the "E". Using your regex the initial question problem happens again, the output gets 3 zeros after "E": Yu.Yu.Hakusho.S01E001 - Death.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.