help with {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)}

All about user-defined episode / movie / file name format expressions
Post Reply
nzdreamer55
Posts: 162
Joined: 20 Jul 2012, 23:25

help with {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)}

Post by nzdreamer55 »

Hello everyone,

I have cobbled together a naming scheme from other peoples suggestions. If I have the word The, A, or An in the beginning, I like to have it be at the end of the title after a coma (for example the big lebowski => big lebowski, the) I was using {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)} to do this, but sometimes filebot will put the files into a folder called $2, $1 and I am not sure why. Here is what I think is happening
n = name
replaceFirst is the command to replace the first word it finds
Not sure about the ^(?i)
(The|A|An) are the things it is looking for to replace
\s(.+) not sure
and don't know what the $2, $1 is for but I think it is the pattern that gets filled in from the first part of the command.

I only get a little of the regex so any help would be appreciated.

Thanks
nzdreamer55
Posts: 162
Joined: 20 Jul 2012, 23:25

Re: help with {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)}

Post by nzdreamer55 »

Should it maybe

{n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /$2, $1/)}
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: help with {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)}

Post by kim »

tip #1 Read up on Regular expression
test out here: https://regexr.com/

btw: I think this is better:

Code: Select all

{ny.replaceFirst(/^(?i)(The|A|An)\s((?:.+(?=.+\(\d{4}))|(?:.+))/, /$2, $1/)}
e.g. output: Movie, The (2020)
vs

Code: Select all

{ny.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /$2, $1/)}
e.g. output: Movie (2020), The

Regular expression:
(abc) capture group
\1 backreference to group #1 ($1 in Filebot)
. Dot. Matches any character except line breaks.
+ Quantifier. Match 1 or more of the preceding token.
User avatar
rednoah
The Source
Posts: 22986
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: help with {n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /\$2, \$1/)}

Post by rednoah »

Just using n.sortName() is an easy option.
:idea: Please read the FAQ and How to Request Help.
Post Reply