|  12-24-2014, 05:16 PM | #1 | 
| New old guy  Posts: 69 Karma: 10 Join Date: Feb 2012 Device: kindle fire | 
				
				regex-function convert roman numerals
			 
			
			Has anyone created a function to convert roman numerals to Arabic, and vice versus? Or help me to learn how to create such a function? | 
|   |   | 
|  12-24-2014, 05:25 PM | #2 | 
| Resident Curmudgeon            Posts: 80,746 Karma: 150249619 Join Date: Nov 2006 Location: Roslindale, Massachusetts Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3 | 
			
			There is no way to convert I to 1 without also converting the word I. So that's out for sure. You would have to do I by doing a manual search/replace.
		 | 
|   |   | 
|  12-24-2014, 06:19 PM | #3 | |
| null operator (he/him)            Posts: 22,012 Karma: 30277294 Join Date: Mar 2012 Location: Sydney Australia Device: none | Quote: 
 Maybe it could be wrangled onto a regex function; BR | |
|   |   | 
|  12-24-2014, 09:09 PM | #4 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			It should be fairly trivial, I outlined a solution in this thread: https://www.mobileread.com/forums/sho...d.php?t=244255 for uppercasing roman numerals. Converting them to arabic is similar, a bit of googling will find you a python function to do the actual conversion. The only major problem would be dealing with small numbers, the dictionary based approach will probably not work well for numbers of less than three digits, so you would need to special case them. | 
|   |   | 
|  12-25-2014, 04:29 AM | #5 | 
| Interested in the matter            Posts: 421 Karma: 426094 Join Date: Dec 2011 Location: Spain, south coast Device: Pocketbook InkPad 3 | 
			
			What I use. Arabic to Roman Search (eg): (>Chapter )(\d+)(<) Regex-function: Code: def num_roman(input):
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = ""
    input=int(input)
    for i in range(len(ints)):
        count = input//ints[i]
        result += nums[i] * count
        input -= ints[i] * count
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(num_roman(match.group(2)))+match.group(3)Search (eg): (>Chapter )(.+?)(<) Regex-function: Code: numeral_map = zip(
    (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
    ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
)
	   
def arabic_num(n):
    n = unicode(n).upper()
    i = result = 0
    for integer, numeral in numeral_map:
        while n[i:i + len(numeral)] == numeral:
            result += integer
            i += len(numeral)
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(arabic_num(match.group(2)))+match.group(3) | 
|   |   | 
|  12-25-2014, 08:16 AM | #6 | |
| Resident Curmudgeon            Posts: 80,746 Karma: 150249619 Join Date: Nov 2006 Location: Roslindale, Massachusetts Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3 | Quote: 
 | |
|   |   | 
|  12-25-2014, 08:40 AM | #7 | |
| Interested in the matter            Posts: 421 Karma: 426094 Join Date: Dec 2011 Location: Spain, south coast Device: Pocketbook InkPad 3 | Quote: 
 Chapter 22, which changes to Chapter XXII. O Chapter XXII, which changes to Chapter 22. Also for the notes, as [22] changes to [XXII] and vice versa. As you can see, I will not go to fix anything later. It is a matter of imagination and cleverness to search.   Last edited by jbacelar; 12-25-2014 at 03:20 PM. Reason: My bad English | |
|   |   | 
|  12-25-2014, 09:32 AM | #8 | |
| Resident Curmudgeon            Posts: 80,746 Karma: 150249619 Join Date: Nov 2006 Location: Roslindale, Massachusetts Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3 | Quote: 
 | |
|   |   | 
|  12-25-2014, 10:27 AM | #9 | |
| Well trained by Cats            Posts: 31,251 Karma: 61360164 Join Date: Aug 2009 Location: The Central Coast of California Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A | Quote: 
 If you want to be sloppy, I guess you would have to worry about your "I worry that this will become..." "1 worry that this became..." | |
|   |   | 
|  12-25-2014, 11:24 AM | #10 | ||
| null operator (he/him)            Posts: 22,012 Karma: 30277294 Join Date: Mar 2012 Location: Sydney Australia Device: none | Quote: 
 The editor provides a checkpointing facility for use by witful users who want to dodge the bullets they aim at their feet, but in the final washup there is nothing one can do to protect the witless from their witlessness. Quote: 
  BR Last edited by BetterRed; 12-25-2014 at 11:40 AM. | ||
|   |   | 
|  12-25-2014, 10:31 PM | #11 | 
| New old guy  Posts: 69 Karma: 10 Join Date: Feb 2012 Device: kindle fire | 
			
			Thanks.  Works for me -- as long as you 'replace all' Nice piece of work | 
|   |   | 
|  09-22-2021, 05:15 PM | #12 | |
| Nameless Being | Quote: 
 | |
|   | 
|  | 
| Thread Tools | Search this Thread | 
| 
 | 
|  Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
| Book detail area: series number given in Roman numerals | BeccaPrice | Calibre | 19 | 09-08-2015 11:42 PM | 
| Roman Numerals for series and on book jacket | Arbait | Library Management | 31 | 05-17-2015 01:16 AM | 
| Disabling roman numerals in series display? | MelBr | Calibre | 2 | 09-19-2013 10:49 PM | 
| Convert Roman numerals to Arabic? | Peter W | Sigil | 2 | 04-09-2012 11:55 AM | 
| regex search for roman numerals | Blurr | Calibre | 2 | 12-16-2009 05:55 PM |