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 10-23-2020, 09:34 PM   #1
GlennMaples
Member
GlennMaples began at the beginning.
 
Posts: 17
Karma: 10
Join Date: May 2010
Device: none
Regex Issues (v4.23)

I'm on 4.23 -- waiting a bit to go to 5.X. But hoping someone would help me with a sanity check on my regex?

I have files where the series are displaying with extra brackets. Displayed in stock series column as:

[My Little Pony 01] [01]
[My Little Pony 02] [01]
[My Little Pony 03] [01]

My regex-->

Search field: Series
Destination field: Series_Index
Search For: (\d{2})
Replace: \1

This is about as simple it could be (infamous last words) and works in the regex101 online engine. But in 4.23 this is returning the entire Series (e.g.,
[My Little Pony 01] instead of the desired number: 02. I have never done regex in python and am hanging my head very low in my abject shame. thanks for your time. :-)

-glenn
GlennMaples is offline   Reply With Quote
Old 10-24-2020, 03:01 AM   #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,832
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
you told it to search for a field with only 2 digits
You really want a field with somthin a space and 2 digits

Code:
.+?\s(\d{2})
theducks is offline   Reply With Quote
Old 10-24-2020, 07:46 AM   #3
GlennMaples
Member
GlennMaples began at the beginning.
 
Posts: 17
Karma: 10
Join Date: May 2010
Device: none
Thank you very much for your quick help.

Been a very long time since any regex -- and I was working with what the regular expressions 101 engine validated w\o thinking about what I was doing. :-(
Your code showed me to account for the string up to the two digits: .+?

works with or without space if I get that right.
GlennMaples is offline   Reply With Quote
Old 10-24-2020, 08:16 AM   #4
GlennMaples
Member
GlennMaples began at the beginning.
 
Posts: 17
Karma: 10
Join Date: May 2010
Device: none
But, if I do as you suggested -- I don't end up with a trailing space. thanks again. :-)
GlennMaples is offline   Reply With Quote
Old 10-24-2020, 09:35 AM   #5
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,832
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by GlennMaples View Post
T
Your code showed me to account for the string up to the two digits: .+?

works with or without space if I get that right.
\s a space
\s+ 1 or more of them
\s* optional or more

My .+? was to keep it from getting too Greedy
I wanted the pattern to always end in space 2digits
theducks is offline   Reply With Quote
Old 10-24-2020, 10:24 AM   #6
GlennMaples
Member
GlennMaples began at the beginning.
 
Posts: 17
Karma: 10
Join Date: May 2010
Device: none
One final note: I had some series with both a space before the number as well as no space:

[My Little Pony 02]
[My Little Pony03]

So I used conditional Regex like this to handle both cases.

Group 6 gives the extracted series name
Group 7 gives the extracted series_index

(\[(.+)?(\s\d{2}))(?(3)\[(.+)?(\d{2}))\]|\[(.+)?(\d{2})\]

This strikes me as being too verbose, but it seems to work.
GlennMaples is offline   Reply With Quote
Old 10-24-2020, 10:29 AM   #7
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,832
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
you could have simply changed my original \s to \s* (assumes title has no digit pairs)
theducks is offline   Reply With Quote
Reply

Tags
calibre, metadata, regex


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Predefined regex for Regex-function sherman Editor 3 01-19-2020 05:32 AM
Image Issues EPUB Text Issues MOBI CLBookworm Conversion 6 09-08-2018 03:35 AM
Need help with RegEx Gary Friedman Editor 8 01-26-2017 08:37 AM
regex help please thevoiceofcheese Calibre 2 08-01-2011 11:27 PM
Charging Issues and Screen Issues srj321 Sony Reader 2 07-11-2010 11:52 PM


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


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