Reputation: 1424
I want to replace the ,
by a .
if both the following cases are true:
,
should be present only once in the string,
should be followed by a maximum of two digitsThese are OK: 1 000 000,51
, 1.000,9
These are not: 9,523,036.11
, 1,000
My evolution so far: https://regex101.com/r/njuKtb/1
Upvotes: 1
Views: 46
Reputation: 103844
You can do:
/^(?!^[^,\n]*,[^,\n]*,[^,\n]*)(?:[^,\n]*),(?=\d{1,2}\D*$)/m
Which is:
^ Start of string or line
(?!^[^,\n]*,[^,\n]*,[^,\n]*) Only matches lines with a single ','
(?:[^,\n]*) Suck up the LH before the ,
, The ,
(?=\d{1,2}\D*$) no more than two \d before end of the line
Upvotes: 0
Reputation: 785156
You may use this regex for search:
^([^,]*),(?=\d{1,2}(?!\d))(?!.*,)
And use this replacement:
$1.
RegEx Details:
^([^,]*)
: Match 0 or more non-comma characters at the start,
: Match literal comma(?=\d{1,2}(?!\d))
: Match 1 or 2 digits not followed by another digit(?!.*,)
: Make sure we don't have comma aheadAlternatively use this for search:
^([^,]*),(?=\d{1,2}(?!\d))([^,\n]*)$
and replace by:
$1.$2
Upvotes: 1