|
|
Thread Tools | Search this Thread |
08-05-2015, 03:37 PM | #1 |
Enthusiast
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 |
08-05-2015, 03:38 PM | #2 |
Enthusiast
Posts: 46
Karma: 10
Join Date: Sep 2014
Device: ipad air
|
attached screenshot
|
08-06-2015, 01:40 PM | #3 |
Ex-Helpdesk Junkie
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\) Code:
jim (ed) & bob & bill (ed) Code:
([^&]+?) \(ed\) |
08-06-2015, 01:42 PM | #4 |
Ex-Helpdesk Junkie
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. |
08-07-2015, 12:30 AM | #5 |
Enthusiast
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? |
08-07-2015, 01:17 AM | #6 |
Ex-Helpdesk Junkie
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 |
08-07-2015, 01:44 AM | #7 |
Ex-Helpdesk Junkie
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\)) Strips out:
P.S. Sure is an ugly-looking regex. Last edited by eschwartz; 08-07-2015 at 01:52 AM. |
08-08-2015, 12:41 PM | #8 |
Enthusiast
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
|
08-08-2015, 01:00 PM | #9 |
Grand Sorcerer
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. |
08-08-2015, 10:33 PM | #10 | |
Ex-Helpdesk Junkie
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:
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. |
|
Tags |
reg expressions, regex, search & replace |
|
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 |