Home > Software design >  Simple regex help - extract price/cost only once (have tried dozens of threads.)
Simple regex help - extract price/cost only once (have tried dozens of threads.)

Time:06-17

I give up! After many many hours, dozens of StackOverflow threads and hundreds of attempts, I am finally turning to the lovely people here for something which will likely take someone else a few seconds to crack.

I have been pulling my hair out over this for hours so deeply appreciate it if anyone can spare a few seconds (which will then allow me to pass that knowledge on in the future.)

TL;DR - I'm trying to extract any currency from any of the following:

 Item price: £93.00 Item number: 265722305071

Item number: 265722305071 Item price: $93.00 

 £93.00£93.00
 
265722305071£93.00 foo

265722305071-93.00EURO

Target response: 93.00

These can be ignored but a tiny selction of some of my attempts:

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

/^£?[1-9]{1,3}(,\d{3})*(\.\d{2})?$/

/^£?(([1-9]{1,3}(,\d{3})*(\.\d{2})?)|(0\.[1-9]\d)|(0\.0[1-9]))$/

/^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/

/^\$?[0-9][0-9,]*[0-9]\.?[0-9]{0,2}$/i

(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d )?(\.\d{1,2})?$

/(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|0)?(\.[0-9]{1,2})?$/

[0-9] \.[0-9][0-9](?:[^0-9a-zA-Z\s\S\D]|$)

^\d \.\d{2}$

I'll then use a separate regex to extract just the currency.

I believe the problem may lie in not knowing exactly what regex engine is being used. The tool I'm using is n8n but I can't find what regex flavour it uses anywhere. Everything else is using Javascript which is where I started.

Massive thanks in advance and warmest regards,

CodePudding user response:

You can use this regex

(?:[$£])(\d (?:\.\d )?)

This regex will capture all float and integer numbers that start either with the $ or £ sign.

Regex Explanation

  • (?: Non-capturing group
    • [$£] Match either $ or £ sign
  • ) Close non-capturing group
  • ( Capturing group
    • \d Match one or more digits
    • (?: Non-capturing group
      • \.\d Match one or more digits with a . (dot) prefix
    • ) Close non-capturing group
    • ? The previous group can exist or not
  • ) Close group

Edit Note

As you mentioned below, a string can be followed by EUR or or even start with these components. So you can use (?<=[$£€]|EUR\s?)\d \.\d |\d \.\d (?=[$£€]|\s?EUR) for that.

function extractPrice(string) {
  let match = string.match(/(?<=[$£€]|EUR\s?)\d \.\d |\d \.\d (?=[$£€]|\s?EUR)/);
  return match ? match[0] : match;
}

console.log(extractPrice("Item price: £93.00 Item number: 265722305071"));
console.log(extractPrice("Item number: 265722305071 Item price: $93.00"));
console.log(extractPrice("£93.00£93.00"));
console.log(extractPrice("265722305071£93.00 foo"));
console.log(extractPrice("265722305071EUR93.00"));
console.log(extractPrice("265722305071EUR 93.00"));
console.log(extractPrice("265722305071 93.00 EUR"));
console.log(extractPrice("265722305071 93.00€"));

  • Related