从字符串中提取价格 [英] Extracting price from string

查看:110
本文介绍了从字符串中提取价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简便的方法来从NSString中提取产品的价格. 我尝试过正则表达式,但总是发现某些不匹配的情况.

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.

价格可以是任意数字,包括小数,0 和-1 (有效价格:10、10.99,-1、0).
NSString可以包含一个字符串,例如:@产品价格:$ 10.99"

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"

谢谢!

推荐答案

这将匹配您提供的所有示例

This will match all the examples you have given

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

可选地为-,后接1个数字,可选地后接小数点和另外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.

这篇关于从字符串中提取价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆