简体   繁体   English

字符串中十进制数的正则表达式

[英]RegEx for decimal numbers in string

SetColoursSizes('0', '[Select Colour]', '0', '[Select Size]', 0,585.00,500.00);

This is an example of the string I'd like to match in PHP, and then caputre the last two variables, in case, '585.00' and '500.00'.这是我想在 PHP 中匹配的字符串的示例,然后捕获最后两个变量,以防万一,“585.00”和“500.00”。 Note: these numbers should be flexible in that they could be anything from 0.50 to $1500.00.注意:这些数字应该是灵活的,因为它们可以是 0.50 到 1500.00 美元之间的任何值。

Can anyone help with the PHP/RegEx for this?任何人都可以为此提供 PHP/RegEx 帮助吗?

Cheers, Michael干杯,迈克尔

This regex will work:此正则表达式将起作用:

SetColoursSizes\s*\([^(]+,\s*([0-9.$]+)\s*,\s*([0-9.$]+)\s*\)
$matches = array();
preg_match('~([\d\.]+),\s*([\d\.]+)\);$~', $yourString, $matches);

$matches[1] and $matches[2] should have the two numbers. $matches[1]$matches[2]应该有两个数字。

The following code captures your final two number/decimal arguments in $matches[2] and $matches[3]:以下代码在 $matches[2] 和 $matches[3] 中捕获最后两个数字/十进制 arguments:

<?php
  $matches = array();
  preg_match('/SetColoursSizes\(([^,]+,\s*){5}(\d+\.?\d*),\s*(\d+\.?\d*)/', "SetColoursSizes('0', '[Select Colour]', '0', '[Select Size]', 0,0.50,500.00);", $matches);
  print_r($matches);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM