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 05-06-2020, 05:41 PM   #1
yamusing
Enthusiast
yamusing began at the beginning.
 
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?
yamusing is offline   Reply With Quote
Old 05-06-2020, 08:38 PM   #2
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
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
theducks is offline   Reply With Quote
Advert
Old 05-07-2020, 01:14 AM   #3
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by theducks View Post
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

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"
yamusing is offline   Reply With Quote
Old 05-07-2020, 09:15 AM   #4
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,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.
chaley is offline   Reply With Quote
Old 05-07-2020, 09:45 AM   #5
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by chaley View Post
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.
I've tried using regex search for "(weight:.+)" replace with "{\1}". I got this:

"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?
yamusing is offline   Reply With Quote
Advert
Old 05-07-2020, 10:14 AM   #6
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,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by yamusing View Post
I've tried using regex search for "(weight:.+)" replace with "{\1}". I got this:

"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?
The easiest way to do this is to use template as the source. That makes it easier to delete text across tag boundaries.

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
To delete the text from tags:
Code:
Source: template
Template: {tags}
Search for: ^(.*), weight:.+(, .*)$
Replace with: \1\2
Destination: tags
BTW: your input examples in the first post do not contain {} characters
chaley is offline   Reply With Quote
Old 05-07-2020, 10:54 AM   #7
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by chaley View Post
The easiest way to do this is to use template as the source. That makes it easier to delete text across tag boundaries.

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
To delete the text from tags:
Code:
Source: template
Template: {tags}
Search for: ^(.*), weight:.+(, .*)$
Replace with: \1\2
Destination: tags
BTW: your input examples in the first post do not contain {} characters
I've never used the template functions, how would I input this into there?
yamusing is offline   Reply With Quote
Old 05-07-2020, 10:59 AM   #8
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,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by yamusing View Post
I've never used the template functions, how would I input this into there?
Like this.
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.jpg
Views:	85
Size:	143.8 KB
ID:	179123  
chaley is offline   Reply With Quote
Old 05-07-2020, 11:26 AM   #9
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by chaley View Post
Like this.
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"
yamusing is offline   Reply With Quote
Old 05-07-2020, 12:44 PM   #10
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,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by yamusing View Post
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"
The regular expression needs to account for the position of the desired field in the string. which can be first, middle, or last. For example:
Code:
^(.*?, *|)(weight:.+?|)(,.*?|)$
The above expression doesn't work if there isn't a "weight:...." tag somewhere in the list.

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.
chaley is offline   Reply With Quote
Old 05-07-2020, 01:07 PM   #11
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by chaley View Post
The regular expression needs to account for the position of the desired field in the string. which can be first, middle, or last. For example:
Code:
^(.*?, *|)(weight:.+?|)(,.*?|)$
The above expression doesn't work if there isn't a "weight:...." tag somewhere in the list.

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.
is it possible to excude the "weight:" text from moving to the other field?

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".
yamusing is offline   Reply With Quote
Old 05-07-2020, 01:12 PM   #12
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,785
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by yamusing View Post
is it possible to excude the "weight:" text from moving to the other field?

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".
Yes. Move the parenthesis of the second group to where you want.

I suggest that you do some experimentation with patterns to see how they work.
chaley is offline   Reply With Quote
Old 05-07-2020, 01:19 PM   #13
yamusing
Enthusiast
yamusing began at the beginning.
 
Posts: 43
Karma: 10
Join Date: Jul 2019
Device: none
Quote:
Originally Posted by chaley View Post
Yes. Move the parenthesis of the second group to where you want.

I suggest that you do some experimentation with patterns to see how they work.
Thank you very much. These work just like I wanted, much appriciated for your help.

^(.*?, *|)weight:(.+?|)(,.*?|)$
\2

then

^(.*?, *|)(weight:.+?|)(,.*?|)$
\1\3

Last edited by theducks; 05-07-2020 at 01:56 PM. Reason: noparse
yamusing is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 02:16 PM.


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