Ask A Question

Notifications

You’re not receiving notifications from this thread.

Regexp for prices or how to detect price in a smart way

eelcoj asked in Ruby

Any regexp experts here? I have the following situation:

  • get an invoice
  • scan it for prices and save the highest number
  • success 🍾

I do this with a .scan(/…/). Pretty fast in Ruby. ✨

Format of pricing, and thus why the regexp gets tricky, can be really all over the place, some examples:

€100,-
€ 59,95
$ 100
$ 10.00
29 €
49,95 €
50 $

And any combination of above, including different type of spaces between currency and number :)

Current regexp looks like this, but that doesn't take into account the currency symbol after the price: /([€$]{1}[ \s]?)(\d{1,6}([,.]\d{1,2})?)/.

I am starting to think I need to think outside of using just regexp and need some ruby magic here, but hope any of you smart folks can help with that too.

Edit: just typing this post, I got the idea of separating the two. Scan for currency symbol first and then check for highest price. 💡

Reply

Hey Jack,

Did you ever come up with a clean solution for this?

I think your final edit (separating the two first) is probably the way I would have tried to tackled it, although my regex skills are horrid... Do you account for currency conversion rates to determine if a value is really higher than the other, or is it purely based on the face value?

Reply

Yes, the edit is the way I went for. Per invoice there's just one currency, so no need for conversion there.
I've been upping my regexp skills since this feature.
Really now just need to whip up something to easily test against various cases, since I do it manually now 😅

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.