我解析命令时遇到小数问题。 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
)
)
答案 0 :(得分:5)
\w
类不包含.
,因此“快速”修复是按以下方式添加:
preg_match_all('/[\w.]+|".*?"|(?!\s)\W/', $cmd, $list);
对于允许的时间段,您有其他要求吗? (即仅在数字之间匹配?)。如果是这样,您可能需要使匹配更专业。
对我来说最重要的事情是,如果句点将用作解析器的其他方面的分隔符。如果您提供的RE是您的整个解析器,那么您可能确定。
答案 1 :(得分:2)
这对我有用:
/[a-zA-Z0-9_\.]+|:|,|".*?"/