![]() |
#1 |
Junior Member
![]() 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 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! |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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') |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Junior Member
![]() 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) |
![]() |
![]() |
![]() |
|
![]() |
||||
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 |