通过正则表达式查找字符串中的十进制数字

发布于 2024-11-30 00:15:05 字数 2230 浏览 0 评论 0原文

我在解析命令时遇到小数问题。 12.90 已被错误解析:

// My command
$cmd = 'CREATE product price:12.90, name: "Create me"';

// My parser
preg_match_all('/\w+|".*?"|(?!\s)\W/', $cmd, $list);

输出:

Array
    (
        [0] => CREATE
        [1] => product
        [2] => price
        [3] => :
        [4] => 12 // << problem starts here
        [5] => .
        [6] => 90
        [7] => ,
        [8] => name
        [9] => :
        [10] => "Create me"
    )

我正在寻找此输出:

Array
    (
        [0] => CREATE
        [1] => product
        [2] => price
        [3] => :
        [4] => 12.90 // supposed
        [5] => ,
        [6] => name
        [7] => :
        [8] => "Create me"
    )

那么,我该如何解决这个问题?

编辑:(更好的解决方案)

伙计们,我根据我的问题接受了杰西的回答。但我意识到这对于我的复杂命令来说还不够好。它更好,因为它可以使用小数(如12.90)和别名(如p.price)。所以只要看看我的例子和下面的解析器就可以了。我希望这对某人有帮助。

// My command
$cmd = 'GET `order` -o, product -p 
           LIST o.user_id, o.product_id, p.name, p.price
           REL o.order_id = 3 AND p.price > 12.90'; 

// My complex command:
preg_match_all('/[0-9_\.]+|\w+|".*?"|`.*?`|\'.*?\'|!=|<=|>=|(?!\s)\W/', $cmd, $list); 

// Output:  
Array
(
[0] => Array
    (
        [0] => GET
        [1] => `order`
        [2] => -
        [3] => o
        [4] => ,
        [5] => product
        [6] => -
        [7] => p
        [8] => LIST
        [9] => o
        [10] => .
        [11] => user_id
        [12] => ,
        [13] => o
        [14] => .
        [15] => product_id
        [16] => ,
        [17] => p
        [18] => .
        [19] => name
        [20] => ,
        [21] => p
        [22] => .
        [23] => price
        [24] => REL
        [25] => o
        [26] => .
        [27] => order_id
        [28] => =
        [29] => 3
        [30] => AND
        [31] => p
        [32] => .
        [33] => price
        [34] => >
        [35] => 12.90
    )

)

I got decimal problem while I parsing my command. 12.90 has been parsed wrongly:

// My command
$cmd = 'CREATE product price:12.90, name: "Create me"';

// My parser
preg_match_all('/\w+|".*?"|(?!\s)\W/', $cmd, $list);

Output:

Array
    (
        [0] => CREATE
        [1] => product
        [2] => price
        [3] => :
        [4] => 12 // << problem starts here
        [5] => .
        [6] => 90
        [7] => ,
        [8] => name
        [9] => :
        [10] => "Create me"
    )

I am looking for this output:

Array
    (
        [0] => CREATE
        [1] => product
        [2] => price
        [3] => :
        [4] => 12.90 // supposed
        [5] => ,
        [6] => name
        [7] => :
        [8] => "Create me"
    )

So, how can I solve this problem?

Edit: (the better solution)

Guys, I accepted Jesse's answer according to my question. But I realized it is not good enough for my complex commands. It's better, because it works with both decimals like 12.90 and alias names like p.price. So just look at my example and parser below for it. I hope this help someone.

// My command
$cmd = 'GET `order` -o, product -p 
           LIST o.user_id, o.product_id, p.name, p.price
           REL o.order_id = 3 AND p.price > 12.90'; 

// My complex command:
preg_match_all('/[0-9_\.]+|\w+|".*?"|`.*?`|\'.*?\'|!=|<=|>=|(?!\s)\W/', $cmd, $list); 

// Output:  
Array
(
[0] => Array
    (
        [0] => GET
        [1] => `order`
        [2] => -
        [3] => o
        [4] => ,
        [5] => product
        [6] => -
        [7] => p
        [8] => LIST
        [9] => o
        [10] => .
        [11] => user_id
        [12] => ,
        [13] => o
        [14] => .
        [15] => product_id
        [16] => ,
        [17] => p
        [18] => .
        [19] => name
        [20] => ,
        [21] => p
        [22] => .
        [23] => price
        [24] => REL
        [25] => o
        [26] => .
        [27] => order_id
        [28] => =
        [29] => 3
        [30] => AND
        [31] => p
        [32] => .
        [33] => price
        [34] => >
        [35] => 12.90
    )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-12-07 00:15:05

\w 类不包含 .,因此“快速”修复方法是添加它,如下所示:

  preg_match_all('/[\w.]+|".*?"|(?!\s)\W/', $cmd, $list);

对于允许句点的位置,您还有其他要求吗? (即仅在数字之间匹配?)。如果是这样,您可能需要使匹配更加专业化。

最让我惊讶的是句点是否会用作解析器其他方面的分隔符。如果您提供的 RE 是您的整个解析器,那么您可能没问题。

\w class does not include the . so the "quick" fix is to add it as follows:

  preg_match_all('/[\w.]+|".*?"|(?!\s)\W/', $cmd, $list);

Do you have any other requirements regarding where the period is allowed? (ie only matching when it is between numbers?). If so, you may need to make the matching more specialized.

The biggest thing thing that jumps out at me is if the period would be used as delimiter for some other aspect of your parser. If the RE that you gave is your entire parser you are probably ok.

公布 2024-12-07 00:15:05

这对我有用:

/[a-zA-Z0-9_\.]+|:|,|".*?"/

This works for me:

/[a-zA-Z0-9_\.]+|:|,|".*?"/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文