Reputation: 1061
I'm using the following regular expression in PHP for matching a key:value pair, where key is a word and value is a positive float number:
preg_match('/^(\w+):(?!0\d)\d*(\.\d+)?/i', $string, $match);
If I input a string such as:
Europe:6
print_r($match)
Returns:
Array ( [0] => Europe:6 [1] => Europe )
Omitting the value part.
Any ideas about what I'm doing wrong?
Thanks in advance.
Upvotes: 0
Views: 102
Reputation: 27614
Check this demo jsFiddle
<?php
$string = "europe:6";
preg_match('/^(\w+):(?!0\d)(\d*)?/i', $string, $match);
print_r($match);
?>
Array ( [0] => europe:6 [1] => europe [2] => 6 )
Here i test and modify your Regular Expression
Hope this help you!
Upvotes: 0
Reputation: 8323
If you're trying to capture a value that is a positive floating point paired with some key, I would suggest you try this instead:
preg_match('/^(\w+):([\.\d]+)/i', $string, $match);
Upvotes: 2
Reputation: 59252
Use this regex:
(\w+)=([0-9]+(?:\.[0-9]*)?)
Group 1 will contain key, while Group 2 will contain the value.
Upvotes: 0