0

I have a whole set of books in the format of

A Knight dogs of the Word - Terry Brooks.epub

or

Enders Game - Orson Scott Card.epub

and I want to swap them into the form

Author's name - Book name.epub

I looked on the site and found this:

^(.*)\s+(\w+\s+\w+)$

followed by:

$2 : $1

(I found \2 - \1 produced the result I wanted for SOME of the books but with a trailing - as well).

However, any book with a different structured name such as Orson Scott Card leaves the first name as well and it gets really weird if two people wrote the book with an & between them.

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
  • 2
    Note the info. on the regex tag that says: *Different languages have different regex implementations. So it's wise to mention the language in which you want your regex to work. If you are not specific about the language, do mention it.* What tool/language are you using to do this? – Jon Clements Jan 19 '14 at 17:56
  • Good luck with `Le Passe-muraille - Marcel Aymé.epub` – Casimir et Hippolyte Jan 19 '14 at 18:26

2 Answers2

2

Let me make a couple of assumptions:

  1. The first part of the name cannot contain "-"
  2. All the files end with ".epub"

In which case you can replace:

^(.*?)\s*-\s*(.*?)\.epub$

With "$2 - $1.epub".

Here's a proof of concept in Java (ignore the double backslashes - that's just Java syntax):

public static void main(String[] args) throws Exception {
    final String[] testData = {"A Knight dogs of the Word - Terry Brooks.epub", "Enders Game - Orson Scott Card.epub"};
    final Pattern patt = Pattern.compile("^(.*?)\\s*-\\s*(.*?)\\.epub$");
    for(final String s : testData) {
        final Matcher m = patt.matcher(s);
        if(m.matches()) {
            System.out.println(m.group(2) + " - "+ m.group(1) + ".epub");
        }
    }
}

Output:

Terry Brooks - A Knight dogs of the Word.epub
Orson Scott Card - Enders Game.epub

As others have pointed out regex isn't necessarily the right tool for the job, it's a bit of a sledgehammer walnut problem. There are plenty on UNIX utilities that will do this with much less pain, for example:

  1. mmv
  2. plain bash
Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Hi. I tried your Search and both $2 - $2.epub and $2 - $1.epub. Neither worked. Since I'm using 'Filemenu Tools' Advanced Renamer on a windows PC I'm not sure how to use mmv or plain bash in this context. There are three types of extensions opf epub and jpg. – user3212674 Jan 19 '14 at 20:38
  • @user3212674 please define neither works. Does it not find the files? Does it rename them incorrectly? If there are multiple different extensions you will need to change `\.epub$` to `\.(.*)$` and use the third match group to keep track of the extension. – Boris the Spider Jan 19 '14 at 20:48
  • Hi Boris. I think the software is not complex and doesn't support all of RegEx. I'm testing on a single file at the moment. Sorry about the confusion; the second $2 - $1 should have read \2 - \1 giving the pair neither of which worked in the replace line. The software does some simple RegEx changes but seems to peter out with more complicated ones. Could you explain (as to an idiot!) what your line does step by step and I will test each bit. If you don't mind and have more time to waste on my question. Richard – user3212674 Jan 21 '14 at 13:39
1

Regex is not the right tool for this job. Use Vladimir Lanin's ren, as in

ren '* - *.epub' '#2 - #1.epub'

I hear that mmv, the successor to ren, is also useful.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • Hi. I'm not sure how to implement mmv and ren in the software I'm using; Filemenu Tools' Advanced Renamer or how to implement them on my pc. As I said I'm just starting to use RegEx and am not clear about these other solutions. – user3212674 Jan 19 '14 at 20:42