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 03-15-2015, 07:21 AM   #1
rcross
Junior Member
rcross began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Mar 2015
Device: kindle, ipad, desktop, multiple
Using regex for substring of comments field

I've searched around quite a bit and tried lots of things, so hopefully i'm just missing something minor.

I have some data in the comments field that I would like to display in a custom column, but I can't seem to get it working;

In the comments fields are things Like:
Code:
Lable: Some Info Here
Lable2: More info
that might be on 2 lines

[optional]general descriptive text
And being the comments field, sometimes it has html characters and sometimes not.

What I think I should be able to do is create a custom column of type "custom built from other columns" and using a template

Code:
{comments:re( 'Label: (\w*)' , '\1')}

Which should give me the output of "Some Info Here" in the column, but what I'm getting is the whole comments field. I've tried many different variations, and anytime I don't get an error, I get the same result of the whole comments field in the column.

I've been able to get {comments:substr(10,25)} to work, and I also replicate the same issue trying to apply re() on other columns like title. The closest I could find is this thread https://www.mobileread.com/forums/sho...d.php?t=245895 where the very last post has someone trying to do something similar for a version string they've stored in the comments field, but with no answer.

As a side note - is there any way to test/debug these things without requiring a restart of calibre everytime?

Thanks!
rcross is offline   Reply With Quote
Old 03-15-2015, 09:55 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,440
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
You wrote a Single Function Mode template. In that mode you do not quote strings. However, because the parameters include parenthesis, you should probably use Template Program Mode or General Program Mode.

The pattern must match the entire string so that the replacement replaces it all. In addition because the string is multiline you must tell re that the '.' character can match a newline. Taking these into account, your template in GPM would be
Code:
program:
	re(field('#comment'), '(?ms).*Lable: (.*?)\n.*' , '\1')
As for testing, there are two ways. The first is to edit the template for the column built ... directly on the spreadsheet. Click in any cell in that column and press F2 or whatever means 'Edit' on your system. The second is to use the template tester, which lets you evaluate templates for a given book. You can add the template tester to the context menu or to the tool bar in calibre's preferences / Toolbar
chaley is offline   Reply With Quote
Advert
Old 03-15-2015, 03:21 PM   #3
rcross
Junior Member
rcross began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Mar 2015
Device: kindle, ipad, desktop, multiple
Thanks! Took a little more fiddling (and I accidently overwrote a batch of metadata by mistake), but I got it figured out out.

Big thanks for the
Code:
(?ms)
tip. Even once you told me, it wasn't easy to figure out from the python docs. I'm used to doing php, so also not sure if I ever would've figured that out on my own.
rcross is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Book 'Comments' Field mitch13 Library Management 5 04-19-2014 05:50 AM
Comments field overwrite - is it a 'must'? travger Library Management 2 09-08-2011 12:37 PM
Comments field emeybee Calibre 1 05-27-2010 02:01 PM
Comments field banjomike Calibre 7 04-08-2010 02:18 PM
Comments field hyperact Calibre 3 12-28-2008 01:38 PM


All times are GMT -4. The time now is 11:32 PM.


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