0

Referring to these posts as reference:

Regular expression to match numbers with or without commas and decimals in text

Regex - to find whole numbers only - problem

Could any of you enlighten me as to what's wrong with this code below and how do I make it select WHOLE numbers under varying conditions? I added a comma to the negative lookahead to fulfil additional conditions but it is causing some problems for me where I can't get certain numbers selected?

My question - How do I get the whole numbers circled in red to be selected as well? I apologise if the solution may seem obvious but I'm pretty new to this and can't seem to figure the problem with the code below, thanks for your help!

My question - How do I get the whole numbers circled in red to be selected as well?

((INR|CNY|MYR|USD|CHF|EUR|GBP|HKD|MOP|SGD|\$)\d+(\d|,\d{3})*(?![0-9,.]))
5x Item C $2.5x5, $2.50x1
1 Book $2.55,1x Table $2.59,2x Item A $2000.5x2, 10x Item B $0.5x10
1x Test $2.513, 5x Pear $100x5, $1.430, $1.50, $1.55
10x Tee $0.5x10,2x HKD4,015x2
1x Kettle $50test, 2x Kettle $3.0x, 100x Dogs $5,666,111, 1x Tents $110x1, 5x Pets $500,
10x Shirt $15,000,000x2
15x Pants $3,000x15
100x Paints $1,000,000,000,000x100
$2,000,000,000,000,000
SGD2,000x3
USD2,00
USD2,0
SGD2ttt
CNY5,000, HKD10,000, GBP1,500, EUR1,000x1, GBP1,500
$150,000.0
$1,000,000.55
$1,00.00, $1,0.00
daviswong
  • 15
  • 3

1 Answers1

0
/(?:INR|CNY|MYR|USD|CHF|EUR|GBP|HKD|MOP|SGD|\$)\d{1,3}(?:(?:,\d{3})*|\d*(?:,\d{1,2})?)(?![,.]?\d)/g

regex101: https://regex101.com/r/994H89/1

(?:INR|CNY|MYR|USD|CHF|EUR|GBP|HKD|MOP|SGD|\$) didn't change from what you have.

\d{1,3} allows 1, 2, or 3 digits

(?:(?:,\d{3})*|\d*(?:,\d{1,2})?) matches as many groups of three with a comma as possible or any number of digits (possibly 0) followed by possibly a comma and 1 or 2 more digits. To allow any number of digits (not just 3) between commas, change {3} to +.

(?!\d|[,.]?\d) does not let a string end with or be followed by a comma, decimal point, or nothing if it's followed by a digit.

Skylar
  • 928
  • 5
  • 18
  • Hi Skylar, thank you very much for your help. It seems to work as what I wanted it to be. Looks like I need a little more time to digest your code. Thanks again. – daviswong Nov 17 '19 at 18:58