Jump to content

another RegExp issue


Recommended Posts

What i want to do is to rename files like this:

Original: movie.s03e02.temp.(34sd8fds).2asdlkha.avi

New: movie - s03e02.avi

So basically i want to remove everything after the second dot

To do this i tried to match it against a regexp and then use the matched pattern to build a new filename.

In the code below im testing, if the filename matches against the pattern.

$FileList = _FileListToArray($path, "*.avi")

For $i=1 To UBound($FileList)-1
  $oldName = StringStripWS($FileList[$i], 4)
  $newName = StringRegExp ($oldName, "(^\.+\.){2}", 0)
  ToolTip($oldName & " - " & $newName)
  Sleep(5000)
Next

The Regexp (^\.+\.){2}

should match 1-unlimited characters that are not "dots"

and then the next character should be a single dot.

This whole pattern repeated exact 2 times.

At least, thats what i want it to do. Yet it doesnt seem to be the right syntax

Given that the aviname is movie.s03e02.temp.(34sd8fds).2asdlkha.avi

the tooltip should then display 1. But it doesnt.

Ive been reading the helpfile and documentation, yet i cant figure out why my regexp doesnt match.

I'd be grateful for an explanation why this doesnt work and what i could do to change it :)

Edited by crest
Link to comment
Share on other sites

#include <Array.au3>

$string = "movie.s03e02.temp.(34sd8fds).2asdlkha.avi"

;REGEX
$result = StringRegExp($string,"(.+)\.(.+)\.(?:.+)\.\((?:.+)\)\.(?:.+)",3)
$newString = StringFormat("%s-%s",$result[0],$result[1])
MsgBox(0,"",$newString)

;STRINGSPLIT
$result = StringSplit($string, ".")
$newString = StringFormat("%s-%s",$result[1],$result[2])
MsgBox(0,"",$newString)

By the way, the ^ symbol matches everything NOT in the group.

Edited by weaponx
Link to comment
Share on other sites

ive tried the splitstring variant earlier on (since you posted a similar one in my other thread)

and it worked quite well, but i just had one problem. if you let the program run a second time, then all files that had been altered already would be altered again. And their name would get messes up, because there was only a single dot and not multiple ones.

the other vairiant that you posted seems to work ... i just have to figure out what the regexp actually does.

Regarding that: i thought that "(.+)\." shouldnt work, since in the .+ the normal dots are also incuded, so it would match the whole string.

Then it actually should never reach the escaped dots \. in the regexp.

So why does it work?

EDIT: if i get this right, everything after (.+)\.(.+)\. in your regexp is only "optional", as it would only handle the stuff after the second dot?

After testing the whole regexp you provided, i noticed that it seems to work only on files that have excatly (or at least) 4 dots in the name.

Well, i didnt think that you would take the example aviname that literally, since i actually wanted to get rid of the junk after the second dot.

So if there was another file with only 4 dots (3 + fileextension) in the filename, like movie.s03e02.temp.(34sd8fds).avi or even like this: movie.s03e02.temp.avi

The program wont work.

Im sorry about that, that i wasnt detailed/accurate enough.

So, what i wanted my program to do is to rename a file consistent of an arbitrary amount of substrings separated by dots,

into a file that only uses the first 2 substrings (= get rid of everything behind the second dot)

And the substrings dont have to look exactly like the one i posted above

The parenthesis were just an example. It could have looked like this as well: movie.s03e02.aF3](.d9wl3.ffff22()().2asdlkha.avi

I just wanted to notify that there might be special signs in the string, and not only numbers and letters.

Edited by crest
Link to comment
Share on other sites

I would just use StringSplit() and do a check on element zero to make sure the number of elements is greater than a certain amount. This will let you know which files had already been converted.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...