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
2
  • try this ((INR|CNY|MYR|USD|CHF|EUR|GBP|HKD|MOP|SGD|\$)&*([0-9,.]{1,}))
    – Alberto
    Commented Nov 17, 2019 at 17:46
  • Do you want to match $1,23,456?
    – Skylar
    Commented Nov 17, 2019 at 18:31

1 Answer 1

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.

1
  • 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
    Commented Nov 17, 2019 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.