Radu Chiriac
Radu Chiriac

Reputation: 1424

regexp to match only one occurrence followed by two digits

I want to replace the , by a . if both the following cases are true:

These 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

Answers (2)

dawg
dawg

Reputation: 103844

You can do:

/^(?!^[^,\n]*,[^,\n]*,[^,\n]*)(?:[^,\n]*),(?=\d{1,2}\D*$)/m

Demo

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

anubhava
anubhava

Reputation: 785156

You may use this regex for search:

^([^,]*),(?=\d{1,2}(?!\d))(?!.*,)

And use this replacement:

$1.

RegEx Demo

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 ahead

Alternatively use this for search:

^([^,]*),(?=\d{1,2}(?!\d))([^,\n]*)$

and replace by:

$1.$2

Upvotes: 1

Related Questions