How to extract the currency from the free text with the currency on the right given a text like "19€99"

Viewed 97

*I have the function presented below to extract prices from free text. In the process of onboarding a new client, they have items aimed at the French market where prices sometimes have the currency symbol in a different position, for example "19€99". How would you rewrite or change the way this algorithm works to accommodate this new requirement and future such requirements? Why would you do it the way you propose?

here is the code

`var valid_countries = ["US", "IT"]`
function extractPrice(description, country){
   if (!valid_countries.includes(country)){
      return -1
   }
       if (country == "US") {
         var price = description.match(/\$(\d+\.\d+)/)[1]

       if (price == null) {
         return -1
    }

 return parsreFloat(price)
  }

  if (country == "IT") {
    var price = description.match(/\€(\d+\.\d+)/)[1]

    if (price == null) {
      return -1
    }

    return parsreFloat(price)
  }


}
1 Answers

I'd first try to see if someone has already done this. Parsing free-form text is a nightmare.

Otherwise, based on your example, I'd try to import the raw data into a database and run a select distinct on the free-form price strings to look for a pattern.

In a perfect world the formatting will be consistent which would make parsing trivial.

If it's inconsistent, then you at least have an itemized list of differences to create special cases.

Related