Zarnia
Zarnia

Reputation: 83

Regex to fix bad deliminators at start of filenames

I've been trying to write a regex to correct bad deliminators after the authors name in ebook file names. Using File Renamer in Javascript.

This is what I have so far:

DEMO HERE

^(.*?)\b(- | --| -- |-- | -)\b

The first deliminator in every file name should be " - " These are the types I'm attempting to correct:

Author Name -Series 00 -Title.txt
Author_Name --Series 01 -Title -Genre.txt
File_Name1-- Sometext- more-info (V1.0).txt
Joe Bloggs- Blah
David Cay-Johnston -- Free Lunch.rtf

--------------DO NOT MATCH-------------

Jackie S. Newton-Paines - Blah- The Biography.epub
Fred Bloggs - Series - title.txt

If I change the regex to:

^(.*?)\b(- | --| -- |-- | -| - )\b 

with the REPLACE:

"$1 - "

Then it works but doesn't allow me to see what changes are going to be made in the preview, because it changes every " - " to " - ". (I use the replace "!$1 - " and then sort on preview to see what changes are to be made before applying)

Is there a way to stop the regex matching " - " and anything after, so I can preview the changes? Thanks.

Upvotes: 0

Views: 46

Answers (1)

Eamon Nerbonne
Eamon Nerbonne

Reputation: 48066

Essentially, you don't want your prefix (.*?) to contain the string - - right?

Well, you can ensure that this is the case by failing to match the "any character" . that is at the start of the string - using a negative lookahead. Specifically, replace (.*?) with ((?! - ).)*?.

Your complete regex should then be: ^(((?! - ).)*?)\b(- | --| -- |-- | -)\b

Upvotes: 1

Related Questions