View Single Post
Old 04-16-2019, 04:07 PM   #2
kacir
Wizard
kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.
 
kacir's Avatar
 
Posts: 3,463
Karma: 10684861
Join Date: May 2006
Device: PocketBook 360, before it was Sony Reader, cassiopeia A-20
Code:
(?P<title>.+) [(](?P<author>[^)]+)[)]
There are much more elaborate Regular Expressions, but this one does what you need and is simple enough to read.

Since the parenthesis is used to enclose fields like (?P<title>.+) or other groups of characters in Regular Expression I choose to "escape" the parenthesis used in filename as an author delimiter by creating character class [(] and [)]. I think you could also "escape" parenthesis by a slash like \( and \). Generally [(] is safer choice, because some programs with Regular Expressions use naked parenthesis for grouping, other escaped ones.

Here is one of more elaborate examples that would process file with name. Every time I found a file name it couldn't process I tried to refine it:
Author [(- Series #)] - Title
Series is either in [] parenthesis or ()
Code:
^(?P<author>((?!\s-\s).)+)\s-\s(?:(?:[[(]\s*)?(?P<series>.+)\s(?P<series_index>[\d\.]+)(?:\s*[])])?\s-\s)?(?P<title>[^(]+)(?:\(.*\))?
Do not worry, I wrote it, but now, after several years it is very difficult for me to read it without analysis and formatting to several lines ;-)
kacir is offline   Reply With Quote