使用正则表达式与产品定价尾随货币符号

时间:2017-03-01 00:07:47

标签: php regex preg-match

所以,仍在学习,正则表达式是令人麻木的东西。但我有一个工作正则表达式,在php中preg_match任何数字基于产品定价,遵循货币符号£。这可能会有所帮助,因为我找不到一个工作示例来考虑所有变体(例如千位和小数等)。对正则表达式的任何改进都非常欢迎!

我的问题是为什么数组包含每个数字的3个实例?接下来的“2”是什么意思?

(?<=\£|GBP)((\d{1,6}(,\d{3})*)|(\d+))(\.\d{2})?

功能:

function website($url) {

$xml = new DOMDocument();
if(@$xml->loadHTMLFile($url)) {

        $xpath = new DOMXPath( $xml );
        $textNodes = $xpath->query( '//text()' );

    foreach ( $textNodes as $textNode ) {

        if ( preg_match('/(?<=\£|GBP)((\d{1,6}(,\d{3})*)|(\d+))(\.\d{2})?/', $textNode->nodeValue, $matches, PREG_OFFSET_CAPTURE ) ) {

            $website_prices[] = $matches;
            global $website_prices;
        }
    }
}

print_r正在转储:

    [3] => Array
    (
        [0] => Array
            (
                [0] => 545
                [1] => 2
            )

        [1] => Array
            (
                [0] => 545
                [1] => 2
            )

        [2] => Array
            (
                [0] => 545
                [1] => 2
            )

    )

1 个答案:

答案 0 :(得分:1)

您当前的正则表达式有许多不必要的分组/格式,这是不需要的。以下正则表达式适用于您的情况:

(?<=£|GBP)[\d.,]+

参见 demo / explanation

PHP

(实现)

<?php
   $re = '/(?<=£|GBP)[\d.,]+/';
   $str = '£545 £5450 £54.20 £5450 £545,620 £545,620.96
           GBP545 GBP5450 GBP54.20 GBP5450 GBP545,620 GBP545,620.96';
   preg_match_all($re, $str, $matches);
   print_r($matches);
?>

(输出)

Array
(
    [0] => Array
        (
            [0] => 545
            [1] => 5450
            [2] => 54.20[3] => 5450
            [4] => 545,620
            [5] => 545,620.96
            [6] => 545
            [7] => 5450
            [8] => 54.20
            [9] => 5450
            [10] => 545,620
            [11] => 545,620.96
        )
)