Regular expression to reverse the order of two unknown words separated by a comma

batch-renameregex

I am trying to use ReNamer regular expressions to reverse folder names that use author names. They are set up as LastName, FirstName and I need to change them to FirstName LastName (minus the comma).

In ReNamer regular expressions usage is set up as Expression on one line and Replace on the second. I assume in this case Input then Output. I've tried several solutions from here but could not get anything to work at all. I barely understand the extreme basics of regex so any and all help is much appreciated.

According to file and folder renaming program, ReNamer, regex is the only option of several choices that will do what I need. I tried someones suggestion from another site. In the Expression Line I typed ([^,]*), (.) then in the Replace Line I typed \2 \1. The result changed Duncan, Dave to 2 1.

Best Answer

I don't know ReNamer but following regex logic this should work:

From: "^([^,]*), (.*)"
To  : "\2 \1"

Explanation:

  • initial ^ means 'start of the expression'
  • the () brackets indicate part of the expression that are to be remembered and can be used in the replacement as \1 and \2.
  • [^,]* matches all characters up to but not including a comma (the ^ in this context means 'not-matching')
  • .* matches all remaining characters
  • the first bracketed expression is your LastName and the second bracketed expression is your FirstName