Search code examples
iosregexios7nsscanner

Extracting price from string


I am looking for a short and convenient way to extract a product's price from NSString. I have tried regular expressions, but always found some cases where did not match.

The price can be any number including decimals, 0 and -1 (valid prices: 10, 10.99, -1, 0).
NSString can contain a string like: @"Prod. price: $10.99"

Thanks!


Solution

  • This will match all the examples you have given

    -?\d+(\.\d{2})?
    

    Optionally a -, followed by 1-many digits, optionally followed by a decimal point and 2 more digits.

    If you've got other numbers that are not prices mixed in to the data then I don't think regex can fulfil your needs.