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 08-05-2015, 03:37 PM   #1
masp
Enthusiast
masp began at the beginning.
 
Posts: 46
Karma: 10
Join Date: Sep 2014
Device: ipad air
regexp search/replace not working as expected on individual authors in source field

Hi, I've been using the "search and replace" (bulk edit) dialog with no problems for, but suddenly hit an unexpected behaviour. I'm using "authors" as my search field, and (my custom, multiple-value field) "editors" as destination. I want to copy a subset of the authors in each book as editors. e.g.:

authors: jim (ed) & bob & bill (ed) => editors: bill & jim

I tried the following regexps:

(.+?) \(ed\)
^(.+?) \(ed\)$

but in every case I'm seeing *all* authors copied, e.g.:

authors: jim (ed) & bob & bill (ed) => editors: bill & bob & jim

I can't figure it out. Any ideas? Thanks! Mike
masp is offline   Reply With Quote
Old 08-05-2015, 03:38 PM   #2
masp
Enthusiast
masp began at the beginning.
 
Posts: 46
Karma: 10
Join Date: Sep 2014
Device: ipad air
attached screenshot
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2015-08-05 at 12.36.31 PM.png
Views:	219
Size:	148.1 KB
ID:	140823  
masp is offline   Reply With Quote
Old 08-06-2015, 01:40 PM   #3
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Code:
(.+?) \(ed\)
Matches twice:

Code:
jim (ed)
& bob & bill (ed)
Try using a character set that does NOT include the ampersand:
Code:
([^&]+?) \(ed\)
eschwartz is offline   Reply With Quote
Old 08-06-2015, 01:42 PM   #4
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
I found http://www.regular-expressions.info to be a very helpful regex site, it taught me most of what I know.

I particularly appreciated their step-by-step guide through what the regex engine sees and does.
eschwartz is offline   Reply With Quote
Old 08-07-2015, 12:30 AM   #5
masp
Enthusiast
masp began at the beginning.
 
Posts: 46
Karma: 10
Join Date: Sep 2014
Device: ipad air
Thanks! (I tried using [^&] but made no difference unfortunately.)

Actually my confusion stems not so much from the regexp syntax (I'm fairly familiar with that from my perl days) but because I was under the impression that when applying the regexp to a multi-value field (ie. an &-separated list of values, such as the "authors" field) the regexp would get applied to each individual value, rather to the whole string. In other words (pseudo-code), if field = "bob & jim (ed) & harry", then rather than this:

field.matches(regex)

it would be implemented more like this:

field.split("&").map { matches(regex) }

but that doesn't seem to be happening: did I misinterpret the docs?
masp is offline   Reply With Quote
Old 08-07-2015, 01:17 AM   #6
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
On second thought, I am not sure what I thought you were thinking. Also, that will teach me to make assumptions.


Experimented a bit, and it looks like replacements are applied to the original string, which is then passed to the destination field. Also, you're right -- each value is treated separately.

So what you really want is to delete the non-editors, which I believe requires a negative lookaround.

Last edited by eschwartz; 08-07-2015 at 01:55 AM. Reason: reversed track, followed by rewording
eschwartz is offline   Reply With Quote
Old 08-07-2015, 01:44 AM   #7
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Found it!

Search:
Code:
(^[^()]+?(?! \(ed\))$|\(ed\))
Replace: nothing. Don't replace with anything.


Strips out:
  • any value in the field that doesn't have the string "(ed)" -- blue section
  • the string "(ed)" -- red section


P.S. Sure is an ugly-looking regex.

Last edited by eschwartz; 08-07-2015 at 01:52 AM.
eschwartz is offline   Reply With Quote
Old 08-08-2015, 12:41 PM   #8
masp
Enthusiast
masp began at the beginning.
 
Posts: 46
Karma: 10
Join Date: Sep 2014
Device: ipad air
Aah! Thanks: I would never have figured that out!! Ugly but perfect in context
masp is offline   Reply With Quote
Old 08-08-2015, 01:00 PM   #9
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
BTW: if you want to process a set of multiple items such as authors or tags as one string instead of individual items then use "template" as the source, enter a template that gives you the string, and enter the destination field. The attachment shows an example combining two authors into one. (I don't know why someone would want to do that, but it makes a good example.)

This technique is also useful for combining values from several fields into one.
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.jpg
Views:	196
Size:	158.0 KB
ID:	140873  
chaley is offline   Reply With Quote
Old 08-08-2015, 10:33 PM   #10
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by masp View Post
Aah! Thanks: I would never have figured that out!! Ugly but perfect in context


My pleasure.

@chaley -- I took a stab at attempting some sort of template to solve the problem. Nothing jumped out at me, so I stuck with plain old field mode.

Even without split fields, you still have to modify the original string.
eschwartz is offline   Reply With Quote
Reply

Tags
reg expressions, regex, search & replace


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search not working as expected banid0 Library Management 3 05-05-2015 09:24 AM
Chained search / replace regexp Terisa de morgan Library Management 23 01-26-2015 01:48 PM
Bulk Search & Replace Question (regexp) Mixx Library Management 3 12-12-2013 01:53 AM
Using Search & Replace with Identifiers field BookJunkieLI Calibre 6 11-13-2012 01:20 PM
RegExp in search field Format C: Library Management 4 06-13-2011 11:00 AM


All times are GMT -4. The time now is 06:54 AM.


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