这个RegEx有什么问题可以匹配€,$和£价格?

时间:2018-02-14 21:03:54

标签: regex

我对正则表达式很新。我正在尝试构建一个正则表达式,我可以用它来查找和提取符合某些有效格式的网站的价格。

让我举几个例子:

€ 14,99    ->  14,99
€14,99     ->  14,99
€1.337,80  ->  1.337,80

£ 4.99     ->  4.99
£4.99      ->  4.99
£4,711.99  ->  4,711.99

$ 8.88         ->  8.88
$14.99         ->  14.99
$4,000,711.99  ->  4,000,711.99

我提出了一个迄今为止效果很好的正则表达式:

/(((?<=([$£] ?))((\d{1,3}(,\d{3})+)|\d+).)|((?<=(€ ?))((\d{1,3}(.\d{3})+)|\d+),))\d{2}(?!\d)/g

第一部分允许美元和英镑价格。作为小数分隔符,作为千位分隔符;第二部分与欧元价格相同。并且,倒置。

然而,这个正则表达式发现了一些假阴性,例如

$9,98 -> 9,98
$ 9,98 -> 9,98
£19,98 -> 19,98

这些价​​格不应该与正则表达式相匹配;但是,他们这样做;我不知道为什么:-)似乎(已经浓缩的)部分存在问题

/(?<=([$£] ?))\d+.\d{2}(?!\d)/g

任何对RegEx有更多经验的人都可以请我指出正确的方向,告诉我做错了什么,以及如何解决它?

这里有一堆完整的例子 - &gt; https://jsfiddle.net/e0re1Lhc/

2 个答案:

答案 0 :(得分:3)

你得到误报(不是否定),因为未转义的.匹配任何角色。要仅匹配小数点,请使用\.将其转义。

答案 1 :(得分:0)

根据我原来的评论,您遇到的问题是.是一个特殊字符。在正则表达式中,这匹配任何字符(\n除外,除非另有说明 - 即在许多正则表达式引擎中使用s /单行模式标志)。

此外,请注意,虽然支持已接近,但目前的后端功能在JavaScript中不起作用!目前,只有Chrome 62+支持此令牌。最好的办法是使用捕获组。

我认为您正在寻找以下正则表达式模式。

See regex in use here

(?:€ ?(\d{1,3}(?:\.\d{3})*,\d{2})|[£$] ?(\d{1,3}(?:,\d{3})*\.\d{2}))(?!\d)

同样,一个正则表达式模式也允许没有千位分隔符的数字,你可以使用以下

(?:€ ?((?:\d{1,3}(?:\.\d{3})*|\d+),\d{2})|[£$] ?((?:\d{1,3}(?:,\d{3})*|\d+)\.\d{2}))(?!\d)

展开并运行下面的代码段以查看其正在使用中!

(function() {
  console.log("hallo");

  let priceTags = document.querySelectorAll(".p13n-sc-price");

  var re = /(?:€ ?(\d{1,3}(?:\.\d{3})*,\d{2})|[£$] ?(\d{1,3}(?:,\d{3})*\.\d{2}))(?!\d)/g,
    count = 0;

  priceTags.forEach((e) => {
    let input = e.textContent,
      match;

    while ((match = re.exec(input)) != null) {
      count++;
      console.log(count, "->", match[1] || match[2]);
    }
  });

})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Positive Matches</h1>
<ol>
  <li class="listing"><span class="p13n-sc-price">€ 12,18</span></li>
  <li class="listing"><span class="p13n-sc-price">$ 14.18</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 12.18</span></li>

  <li class="listing"><span class="p13n-sc-price">£12.99</span></li>
  <li class="listing"><span class="p13n-sc-price">£4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€4,75</span></li>
  <li class="listing"><span class="p13n-sc-price">€ 4,75</span></li>
  <li class="listing"><span class="p13n-sc-price">$4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">$ 4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€14,75</span></li>

  <li class="listing"><span class="p13n-sc-price">$1,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">£1,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€1.004,75</span></li>

  <li class="listing"><span class="p13n-sc-price">$ 1,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 1,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€ 1.004,75</span></li>

  <li class="listing"><span class="p13n-sc-price">$ 1,000,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 1,000,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€ 1.000.004,75</span></li>

  <li class="listing"><span class="p13n-sc-price">$ 7.99 - $ 12.18</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 7.99-£ 12.18</span></li>
  <li class="listing"><span class="p13n-sc-price">€ 4,59 - €17,34</span></li>

  <li class="listing"><span class="p13n-sc-price">$9.98 - $14.98</span></li>
  <li class="listing"><span class="p13n-sc-price">£7.99 - £12.18</span></li>
  <li class="listing"><span class="p13n-sc-price">€4,59 - €17,34</span></li>
</ol>

<h1>Negative Matches</h1>
<ol>
  <li class="listing"><span class="p13n-sc-price">€1,004.75</span></li>
  <li class="listing"><span class="p13n-sc-price">$1.004,75</span></li>
  <li class="listing"><span class="p13n-sc-price">£1.004,75</span></li>
  <li class="listing"><span class="p13n-sc-price">€4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">€ 4.75</span></li>
  <li class="listing"><span class="p13n-sc-price">$9,98</span></li>
  <li class="listing"><span class="p13n-sc-price">$ 9,98</span></li>
  <li class="listing"><span class="p13n-sc-price">£19,98</span></li>
  <li class="listing"><span class="p13n-sc-price">£ 19,98</span></li>

  <li class="listing"><span class="p13n-sc-price">$9,98 - $14,98</span></li>
  <li class="listing"><span class="p13n-sc-price">£7,99 - £12,18</span></li>
  <li class="listing"><span class="p13n-sc-price">€4.59 - €17.34</span></li>

  <li class="listing"><span class="p13n-sc-price">$9,98 - $14,98</span></li>
  <li class="listing"><span class="p13n-sc-price">£7,99 - £12,18</span></li>
  <li class="listing"><span class="p13n-sc-price">€4.59 - €17.34</span></li>
</ol>