05-06-2020, 05:41 PM | #1 |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Regex search and replace
I'm trying to capture and move certain text within the tag colums to another column for 100s of books but I find it too complex to do within regex. How can I do the following with the regex search and replace, within the tags column example:
"5-glass, apple, group:95#TR4, Hefine94, weight:140lbs (acc), Z97-A.5" "case:AR5steel, group:Fractal-group, heavy, T*m5%, weight:@eightySix'Kilos" "case:AR5steel, dark, group:R3.R4.R5, group:Fractal-group" I want to capture the characters after "weight:" but not the comma or anything after that, then move that piece of text into another column. After that remove "weight:" and the captured text from the tags column. Is this possible? |
05-06-2020, 08:38 PM | #2 |
Well trained by Cats
Posts: 29,973
Karma: 56143930
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
NOT Tested: Copy to new
\,\s(weight:.+?)\,* Replace: \1 Destination field : <your other column> <<<this is important or you will lose the rest of the tags Search for a comma Space Capture weight an up, but not including a comma (optional) The cleanup is a whole lot trickier AND SERIOUSLY NOT TESTED Make a Backup, there is no Undo I would do as 2 steps First is where Weigh is the last item (this should just truncate it away) (.+)\,\sweight:.+? Replace:\1 Second is similar, but carves out weight and puts the 2 chunks together with a comma space (.+)\,\sweight:.+?\,\s(.+) Replace:\1, \2 |
Advert | |
|
05-07-2020, 01:14 AM | #3 | |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Quote:
I've tried all three but it captures the whole tag line. I have regular expression in search mode. \,\s(weight:.+?)\,* Replace: \1 test case "case:AR5steel, group:Fractal-group, heavy, T*m5%, weight:@eightySix'Kilos" test result "case:AR5steel, group:Fractal-group, heavy, T*m5%weight:@eightySix'Kilos" |
|
05-07-2020, 09:15 AM | #4 |
Grand Sorcerer
Posts: 11,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
On tags (and any other multiple-valued column) Search/Replace regexs operate on a tag-by-tag basis. Tags cannot contain a comma, so you must not look for one.
If you want to operate on tags as a single string then you must use "template" as the source and use {tags} as the template. |
05-07-2020, 09:45 AM | #5 | |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Quote:
"5-glass apple group:95#TR4 Hefine94 {weight:140lbs (acc)} Z97-A.5" "case:AR5steel group:Fractal-group heavy T*m5% {weight:@eightySix'Kilos}" "case:AR5steel dark group:R3.R4.R5 group:Fractal-group" How do I make the search and replace capture everything in the "{}" then move only that not including the parentheses to another field? Then removing the text within "{ }" and parentheses itself in the tag column? |
|
Advert | |
|
05-07-2020, 10:14 AM | #6 | |
Grand Sorcerer
Posts: 11,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
To copy the text to a new column: Code:
Source: template Template: {tags} Search for: ^.*, (weight:.+), .*$ Replace with: \1 Destination: whatever the name of your column is Code:
Source: template Template: {tags} Search for: ^(.*), weight:.+(, .*)$ Replace with: \1\2 Destination: tags |
|
05-07-2020, 10:54 AM | #7 | |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Quote:
|
|
05-07-2020, 10:59 AM | #8 |
Grand Sorcerer
Posts: 11,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Like this.
|
05-07-2020, 11:26 AM | #9 |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
thanks it seems to work for the first field but the others tag fields in my other books aren't working
test: "5-glass, apple, group:95#TR4, Hefine94, weight:140lbs (acc), Z97-A.5" result: "weight:140lbs (acc)" test: "case:AR5steel, group:Fractal-group, heavy, T*m5%, weight:@eightySix'Kilos" result: "case:AR5steel, group:Fractal-group, heavy, T*m5%, weight:@eightySix'Kilos" test: case:AR5steel, light, group:Fractal-group, weight:@seventyOne'Kilos, Xeon, ZRX0, ZRX4" result: "weight:@seventyOne'Kilos, Xeon, ZRX0" |
05-07-2020, 12:44 PM | #10 | |
Grand Sorcerer
Posts: 11,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
^(.*?, *|)(weight:.+?|)(,.*?|)$ I leave it to you to figure out how to change the expression you use to delete the tag from the tag set. The change will be similar. |
|
05-07-2020, 01:07 PM | #11 | |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Quote:
test: "case:AR5steel, light, group:Fractal-group, weight:@seventyOne'Kilos, Xeon, ZRX0, ZRX4" result: "weight:@seventyOne'Kilos" The result am looking for is "@seventyOne'Kilos". |
|
05-07-2020, 01:12 PM | #12 | |
Grand Sorcerer
Posts: 11,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I suggest that you do some experimentation with patterns to see how they work. |
|
05-07-2020, 01:19 PM | #13 | |
Enthusiast
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
|
Quote:
^(.*?, *|)weight:(.+?|)(,.*?|)$ \2 then ^(.*?, *|)(weight:.+?|)(,.*?|)$ \1\3 Last edited by theducks; 05-07-2020 at 01:56 PM. Reason: noparse |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regex in search problems (NOT Search&Replace; the search bar) | lairdb | Calibre | 3 | 03-15-2017 07:10 PM |
Regex: Search and Replace | Thomas_AR | Calibre | 2 | 03-31-2016 06:23 PM |
Regex search and replace | dwlamb | Sigil | 6 | 04-12-2013 02:34 PM |
regex search/replace | Sharlene | Sigil | 10 | 01-28-2012 04:14 AM |
need regex help search and replace | schuster | Calibre | 4 | 01-10-2011 09:00 AM |