View Single Post
Old 04-04-2022, 02:20 PM   #49
greenskye
Member
greenskye began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Feb 2010
Device: none
Spoiler:
Quote:
Originally Posted by lomkiri View Post
Assuming all numbers are in european format (no one in US format):
Code:
find:
\d[,.\d]{2,}(?![^<>{}]*[>}])
function:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): 
    return match.group(0).replace('.', '§').replace(',', '.').replace('§',',')
Warning: if you have a mixture of numbers in both formats (US and european), they will be switched. In that case, you'll have to refine the selection to catch only european ones.

Note: all number with 3 or more positions will be catched (e.g. 1,2, or 1.2). If you want to be more selective, change "{2,} for what you want minus 1, e.g. {4,} if you want to catch starting from 5 positions (1.000 or 12,45)

Note: Integers as 100 or 234000 will be catched, but they won't be transformed.

Warning : numbers followed by 3 dots will be wrongly transformed : "They were 20..." will give "They were 20,,,"
It's wise to change them to ellipsis (…) prior to apply the conversion:
(\d)\.{3} ==> \1\u2026


Thanks so much, it worked great!

I ended up using
Code:
find 1: (\d{1,3}[.,])+\d{1,}(?![^<>{}]*[>}])

find 2: \$(\d{1,3}[.,])+\d{1,}(?![^<>{}]*[>}])
The updated regex fixed the problem with trailing "." matches. I did a replace all with "find 1", and then ran it again with "find 2" to revert any US currencies accidentally caught. Couldn't figure out how to exclude them in first place (kept matching part of the currency number)
greenskye is offline   Reply With Quote