-4

This mightbe a silly question, but I'm new to RegExes. I need a RegEx pattern for a price in my python program.

Sample cases :

8.00
25.14
271.61
2,367.82
52,124.09
9,37,159.82

Any help would be appreciated. Thanks in Advance.

GingerNinja23
  • 164
  • 2
  • 10
  • 1
    I think your last example is formatted badly. A comma should be followed by three digits, you have `9,37,159.82` – Holy Mackerel Dec 25 '13 at 15:27
  • Yes, but amazon.in uses the same kind of pricing pattern and my program scrapes data from amazon.in . – GingerNinja23 Dec 25 '13 at 15:30
  • Please do some research about regexes before you ask a question. If you still didn't succeed, then show what you tried in your question. – gitaarik Dec 25 '13 at 15:31
  • @kaveman: If there is a problem with a post, please describe the problem in detail, instead of just posting "what have you tried". See [this Meta post](http://meta.stackexchange.com/a/172760/152134) on whether "what have you tried" comments are acceptable and alternatives to "what have you tried". Thanks! – Qantas 94 Heavy Jan 25 '14 at 08:06

2 Answers2

2

This one matches all of the above:

(\d{1,3},?)*\d{1,3}\.\d{2}
Victor
  • 840
  • 8
  • 22
0

You are trying to match numbers formatted according to the rules of Indian numbering system. In order to do this correctly you have to carefully define what you would like to treat as a match. For example would string such as 100,10,100.00 constitute a match? You can find a validly formatted number inside this example, i.e. 10,100.00 but I assume that for your purposes string from the example should not match. I elaborated a bit on the tests and came up with this test suite:

# match
8.00
25.14
271.61
2,367.82
52,124.09
9,37,159.82

# don't match
9,378,159.82
9,37,159.820
12,.80
12,12
12.,12
12,12.80
10,80
.80
80,

Following regular expression passes these tests successfully.

(?<![\d,])((\d{1,2},)*\d{3}|(\d{1,3}))\.\d{2}(?!\d)
Mr. Deathless
  • 1,083
  • 11
  • 12
  • Why would: 9,37,159.82 match? – zero_cool Mar 15 '20 at 21:09
  • I'm afraid that I don't quite understand your question, but I'll try to interpret it to the best of my ability. 9,37,159.82 is a properly formatted number accorting to Indian numbering system. Explaining why supplied regular expression matches this particular number, that is writing a step-by-step execution of regex, would be too long for the comment. If you're interested in understanding which steps regex engine will take I suggest using a debugging tool. You can find different options to debug a regex in this question https://stackoverflow.com/questions/2348694/how-do-you-debug-a-regex – Mr. Deathless Apr 03 '20 at 11:43
  • Never seen a price that uses commas after 1 then 2 numbers - that's all. Prices are typically $100,000.00 or something like that. OP didn't mention Indian Numbering system, but I'm not familiar with that either. – zero_cool Apr 03 '20 at 21:36