| 
			
			 | 
		#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?  | 
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#2 | 
| 
			
			
			
			 Resident Curmudgeon 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 80,784 
				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.
		 
		
	
		
		
		
		
		
		
		
		
		
		
	
	 | 
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| Advert | |
| 
         | 
    
| 
			
			 | 
		#3 | |
| 
			
			
			
			 null operator (he/him) 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22,018 
				Karma: 30277294 
				Join Date: Mar 2012 
				Location: Sydney Australia 
				
				
				Device: none 
				
				
				 | 
	
	
	
		
		
		
		
		 Quote: 
	
 Maybe it could be wrangled onto a regex function; BR  | 
|
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#4 | 
| 
			
			
			
			 creator of calibre 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,609 
				Karma: 28549044 
				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.  | 
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#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)
 | 
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| Advert | |
| 
         | 
    
| 
			
			 | 
		#6 | |
| 
			
			
			
			 Resident Curmudgeon 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 80,784 
				Karma: 150249619 
				Join Date: Nov 2006 
				Location: Roslindale, Massachusetts 
				
				
				Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3 
				
				
				 | 
	
	
	
		
		
		
		
		 Quote: 
	
  | 
|
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#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 04:20 PM. Reason: My bad English  | 
|
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#8 | |
| 
			
			
			
			 Resident Curmudgeon 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 80,784 
				Karma: 150249619 
				Join Date: Nov 2006 
				Location: Roslindale, Massachusetts 
				
				
				Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3 
				
				
				 | 
	
	
	
		
		
		
		
		 Quote: 
	
  | 
|
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#9 | |
| 
			
			
			
			 Well trained by Cats 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 31,267 
				Karma: 61916422 
				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..."  | 
|
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#10 | ||
| 
			
			
			
			 null operator (he/him) 
			
			![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22,018 
				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 12:40 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  | 
| 
		 | 
	
	
	
		
		
		
		
			 
		
		
		
		
		
		
		
			
		
		
		
	 | 
| 
			
			 | 
		#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-09-2015 12:42 AM | 
| Roman Numerals for series and on book jacket | Arbait | Library Management | 31 | 05-17-2015 02:16 AM | 
| Disabling roman numerals in series display? | MelBr | Calibre | 2 | 09-19-2013 11:49 PM | 
| Convert Roman numerals to Arabic? | Peter W | Sigil | 2 | 04-09-2012 12:55 PM | 
| regex search for roman numerals | Blurr | Calibre | 2 | 12-16-2009 06:55 PM |