Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 07-26-2020, 04:12 AM   #1
vargr
Member
vargr began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Oct 2019
Device: paperwhite; android
Selecting first names from strings like A B/Q R

Hello, can anyone help with a template I'm trying to make for cover generation? I want it to take a text field that's a series of 2+ names separated by slashes, and return just the first word of each name, retaining the slashes.

i.e., so that "John Doe/Jim Smith" would show as "John/Jim" on the generated cover.
vargr is offline   Reply With Quote
Old 07-26-2020, 05:33 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,445
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by vargr View Post
Hello, can anyone help with a template I'm trying to make for cover generation? I want it to take a text field that's a series of 2+ names separated by slashes, and return just the first word of each name, retaining the slashes.

i.e., so that "John Doe/Jim Smith" would show as "John/Jim" on the generated cover.
One of these should do it. Using general program mode:
Code:
program: list_re(field('#text'), '/', '^(.*)\b.*$', '\1')
Using template program mode:
Code:
{#text:'list_re($, '/', '^(.*)\b.*$', '\1')'}
Change #text to be the lookup name of your text column.

The above also handles single names. For example "John Doe/Fred/Jim Smith" produces "/John/Fred/Jim". If you don't want that then change the \b to a space character.

Last edited by chaley; 07-26-2020 at 12:09 PM.
chaley is offline   Reply With Quote
Old 07-26-2020, 08:39 AM   #3
vargr
Member
vargr began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Oct 2019
Device: paperwhite; android
Quote:
Originally Posted by chaley View Post
One of these should do it. Using general program mode:
Code:
program: list_re(field('#text'), '/', '^(.*)\b.*$', '\1')
Using template program mode:
Code:
{#text:'list_re($, '/', '^(.*)\b.*$', '\1')'}
thanks for the quick reply! unfortunately both of these are giving me the full names still...
vargr is offline   Reply With Quote
Old 07-26-2020, 09:01 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,445
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by vargr View Post
thanks for the quick reply! unfortunately both of these are giving me the full names still...
Oops. That will teach me to "improve" the regex without testing it.

The correct templates are
Code:
program: list_re(field('#text'), '/', '^(..*?)\b.*$', '\1')
and
Code:
{#text:'list_re($, '/', '^(..*?)\b.*$', '\1')'}
chaley is offline   Reply With Quote
Old 07-26-2020, 09:11 AM   #5
vargr
Member
vargr began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Oct 2019
Device: paperwhite; android
perfect, thank you so much!
vargr is offline   Reply With Quote
Old 07-26-2020, 11:04 AM   #6
vargr
Member
vargr began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Oct 2019
Device: paperwhite; android
After testing a few files with this, I have some more questions...

1) Is it possible to return the shortened form for both when there's more than one pair of names in the field separated by a comma?

2) Can it account for " & " in place of "/", i.e., "John Doe & Jim Smith" to "John & Jim"? (This isn't as big a deal, but I do have some fields like that.)
vargr is offline   Reply With Quote
Old 07-26-2020, 12:10 PM   #7
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,445
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Moderator Notice
Moved from the sticky recipes thread
chaley is offline   Reply With Quote
Old 07-26-2020, 12:20 PM   #8
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,445
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by vargr View Post
After testing a few files with this, I have some more questions...

1) Is it possible to return the shortened form for both when there's more than one pair of names in the field separated by a comma?
This turns commas into slashes then computes the list of first names.
Code:
program: list_re(re(field('#text'), ',', '/'), '/', '^(..*?)\b.*$', '\1')
2) Can it account for " & " in place of "/", i.e., "John Doe & Jim Smith" to "John & Jim"? (This isn't as big a deal, but I do have some fields like that.)[/QUOTE]I assume that the spaces are around the names in the source and that you want them in the result. The second parameter to list_re is the separator. Change '/' to ' & '.
Code:
program: list_re(field('authors'), ' & ', '^(..*?)\b.*$', '\1')
chaley is offline   Reply With Quote
Old 07-26-2020, 08:22 PM   #9
vargr
Member
vargr began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Oct 2019
Device: paperwhite; android
Thanks again! I ended up making a second custom column for the pairs separated by & so my covers can show both shortened forms when both are present. (This probably sounds overcomplicated, but in fanfiction tagging, / and & are semantically different, and sometimes a story includes both with different pairs of names, so...)
vargr is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pen names and real names whiteowl Library Management 4 01-19-2020 06:22 PM
automatic changing christian names and family names sjefclaassen Calibre 3 09-29-2017 06:59 AM
Braces in strings Jellby Development 4 09-08-2014 01:10 PM
How to exclude strings before and after ElMiko Sigil 14 07-21-2012 06:34 PM
What are these strings for? Jellby Calibre 2 05-22-2011 01:08 PM


All times are GMT -4. The time now is 02:10 PM.


MobileRead.com is a privately owned, operated and funded community.