我试图从任何给定的URL抓取价格。我正在使用CsQuery,无论如何,我找不到在一个页面上找到所有可能是价格的项目的最好方法。额外的好处是根据测试的大小/颜色计算出最可能的价格,以及它离页面顶部有多近。我在想也许可以考虑Regex解决方案,但我不确定这是否是使用CsQuery的正确方式。
发布于 2015-02-23 05:59:22
嗯,如果有一个货币符号,你可能会这样做。
(?:\$|\£)(\d+(?!\d*,\d)|\d{1,3}((, ?)\d{3}?)?(\3\d{3}?){0,4})(\.\d{1,2})?(?=[^\d,]|, (?!\d{3,})|$)
(?:\$|\£) -- matches literal currency simbols. You can remove this
if you can't count on the presence of currency symbols,
but it's a great anchor if you can
(\d+ -- matches any number of digits
(?!\d*,\d) as long as not followed by comma digit
|
\d{1,3} -- otherwise matches betweein 1 and 3 digits
(
(, ?) -- looks for a comma followed by a possible space
captures as \3
\d{3}?) -- followed by 3 digits
? -- zero or one times
(\3 -- looks for the same pattern of comma with or without space
\d{3}? -- followed by 3 digits
){0,4}) -- between 0 and 4 times, more on that below
(\. -- literal period
\d{1,2} -- followed by one or two digits
)? -- zero or one times (so, optional)
(?=[^\d,]|, (?!\d{3,})|$)你可以做的另一件事是限制逗号组的重复次数,这可能有助于剔除可能不是价格的高数字。如果你的期望值不超过999999,你可能会这样做(但如果你处理的是外币,通货膨胀已经造成了一些天文数字的高--在辛巴威,一条面包要卖5000万美元)。
为了便于阅读,我将向您展示如何将重复次数限制为7次
将4 (整个正则表达式中唯一的4)更改为6 (您想要的数字-1,因为我们预先查找1以建立逗号模式)。
(?:\$|\£)(\d+(?!\d*,\d)|\d{1,3}((, ?)\d{3}?)?(\3\d{3}?){0,6})(\.\d{1,2})?(?=[^\d,]|, (?!\d{3,})|$)您可以在以下位置查看实际操作:https://regex101.com/r/oU2nW2/1
https://stackoverflow.com/questions/28662016
复制相似问题