Thread: Regex examples
View Single Post
Old 07-26-2020, 03:00 AM   #658
Klecks
Enthusiast
Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.Klecks never is beset by a damp, drizzly November in his or her soul.
 
Klecks's Avatar
 
Posts: 39
Karma: 59154
Join Date: May 2010
Location: Stuttgart, Germany
Device: Kobo H2O, PocketBook Touch HD, Tolino Vision 4
Hi luciaisacat,

I have played with the idea from davidfor and came up with the following solution for the calibre editor:

1. open your file in the calibre editor.
2. call search/replace, change to Mode: "regex-function", insert your search string and then click "create/edit"
3. you will see a basis function.
4. insert a name for your new Function and replace the code with:
Code:
import re
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    #call file_name and strip file path and -extension:
    newName=(re.search(r'.*?\/([^\.\/]*)\.[^\.]*', file_name).group(1))
    newName=re.sub(r'_',r' ',newName)        #strip _
    newName=re.sub(r' 0',r' ',newName)        #strip leading zero
    #replace bracket term with newName:
    result=re.sub(match.group(1),newName,match.group())
    return result
5. confirm with OK and now you can use that function. It will replace whatever is between the brackets in your search string with the file name (minus path and file extension)


Klecks
Attached Thumbnails
Click image for larger version

Name:	Step 2.JPG
Views:	410
Size:	56.0 KB
ID:	180929   Click image for larger version

Name:	Step 3 - basis function.JPG
Views:	396
Size:	90.8 KB
ID:	180930   Click image for larger version

Name:	Step 4 - new function.JPG
Views:	410
Size:	140.3 KB
ID:	180931  
Klecks is offline   Reply With Quote