Search code examples
regexregex-lookarounds

Extract price/cost only once


I'm trying to extract the price 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 are a selection 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 use a separate regex to extract the currency.

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


Solution

  • 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€"));