-5

As the question says so that in the following string:

"2 x 500ml 1664 êcole beer £8.0"

i would get 2 and 1664 returned only.

Ive tried: [1-9][0-9]* which is close but returns the 500 in 500ml and the 8 in #8.0

The idea is to return the quantity when reading a line in a receipt, hence the example above.

Wrumble
  • 231
  • 1
  • 5
  • 15
  • Possible duplicate of [Get numbers from string with PHP](https://stackoverflow.com/questions/11243447/get-numbers-from-string-with-php). Also, what have you tried? There's surely a better duplicate out there, but this is the one I found with little research – ctwheels Sep 26 '17 at 14:02
  • 1
    Didn't you just ask a similar question here https://stackoverflow.com/q/46424370/6124528? – Manav Sep 26 '17 at 14:06
  • 1
    I'd suggest you learn regex. As @Manav pointed out, you're asking a few regex questions that a very similar. While the stackoverflow is more than willing to help you, we are not a coding service to write your code for you. – ctwheels Sep 26 '17 at 14:07
  • Yes, but im looking to get just the integers. From here i can get the first integer which would more than likely be a quantity – Wrumble Sep 26 '17 at 14:09
  • Hi you're right i do need to. But ive just been unsuccesful for a while now. I feel its better to learn this in my free time though. – Wrumble Sep 26 '17 at 14:10
  • There's no clear-cut way to get just integers when you're talking about multiple possible strings. If you present us with a multitude of strings and the values you want, we can probably get them for you. For example, if you have a string of `This is number 1. This is number 2.` - do we need to parse this? Is this a possible string? According to what you've presented, you need this regex: `(?<=\s|^)(\d+)(?=\s|$)` but this will only work for specific strings (such as the one you presented). It's also important to specify the language you are using since each language has different regex support – ctwheels Sep 26 '17 at 14:15
  • The best way might be to split the string by space and remove all non-digit characters and then, with code, determine whether or not they are integers, floats, etc. – ctwheels Sep 26 '17 at 14:16
  • Right ok, im using swift. And the idea is to get a quantity from reading a line in a receipt which is normally the first integer. – Wrumble Sep 26 '17 at 14:29

1 Answers1

0

Using Python, I would try to get the each number separately like this:


First occurrence:

"(\d{+})\ x"

enter image description here


Second occurrence:

"\ (\d{4})\ "

enter image description here

Now you'll just have to find a way to get the numbers that are inside each group.. ;-)


EDIT:

You can get the numbers using a one-line solution, and then fetch each subgroup to access the numbers.

One-line Solution:

"(\d+)\ "

enter image description here

dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • 1
    Isn't `\d{1}` sort of redundant? Also, why don't you create a one-liner – ctwheels Sep 26 '17 at 14:18
  • You're correct! But since I'm mostly a python programmer I prefer to let things very explicit.. :P – dot.Py Sep 26 '17 at 14:19
  • About the one-line solution, can you show us how we could achieve this? – dot.Py Sep 26 '17 at 14:20
  • 1
    `\d|\d{4}`, but realistically you should use `\d+` since there is potentially a possibility of multiple digit numbers like `10 x ...`, etc. – ctwheels Sep 26 '17 at 14:21
  • Good point! Changed `"(\d{1})\ x"` to `"(\d{+})\ x"`. Also added `"(\d+)\ "` as a one-line solution based on your comment. Thanks. – dot.Py Sep 26 '17 at 14:22