1

我正在尝试从任何给定的 URL 中获取价格。我正在使用 CsQuery,在我的一生中,我无法找出在页面上找到可能是价格的所有项目的最佳方法。奖金将通过测试的大小/颜色以及它与页面顶部的接近程度来确定最可能的价格。我在想也许正在寻找一个正则表达式解决方案,但我不确定这是否是使用 CsQuery 的正确方法。

4

1 回答 1

1

好吧,如果存在货币符号,您可能会执行类似的操作。

(?:\$|\£)(\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,})|$)

您可能会做的另一件事是限制逗号组的重复次数,这可能有助于清除不太可能价格的高数字。如果您不期望超过 999,999,您可能会这样做(但如果您处理的是外币,通货膨胀率已经达到了天文数字——津巴布韦的一条面包价值 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

于 2015-02-22T21:59:22.313 回答