alien73 Posted May 22, 2010 Share Posted May 22, 2010 Hi there, How would I change the reg expressions to allow any number format like 1.23 10.23 100.23 1000.23 Right now I have the code to match a ( - or +) then a number, digit then 2 numbers. preg_match_all("/[-|+]\s\d{1}\.\d{2}/", $sym_price, $matches); Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/ Share on other sites More sharing options...
JAY6390 Posted May 22, 2010 Share Posted May 22, 2010 You mean multiple numbers before the decimal? preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062044 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 Not tested, but how about this? /^[+-]?\d+\.\d+$/ Or if you only want the non-fractional part (is there a word for that) to be a multiple of 10, then perhaps like this: /^[+-]?10*\.\d+$/ Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062046 Share on other sites More sharing options...
alien73 Posted May 22, 2010 Author Share Posted May 22, 2010 Thanks, but it doesn't work perhaps it's because it's not checking for a space after the - or + sign? Not sure? Sorry for got to mention that.. oops I have a text box and I want preg_match_all to check for the - or + sign then a space then the price. I'm setting up a down and dirty product options with prices feature - 5.00 + 25.00 +105.00 etc... Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062049 Share on other sites More sharing options...
alien73 Posted May 22, 2010 Author Share Posted May 22, 2010 You mean multiple numbers before the decimal? preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches); correct! Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062050 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 You mean multiple numbers before the decimal? preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches); correct! Heh... are you sure? <?php $sym_price = 'My name is Daniel| 5.00 hello world '; if (preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches)) { echo 'It matches'; } Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062051 Share on other sites More sharing options...
alien73 Posted May 22, 2010 Author Share Posted May 22, 2010 Thanks it works.. Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062052 Share on other sites More sharing options...
JAY6390 Posted May 22, 2010 Share Posted May 22, 2010 It will only match so long as there is a + or - before it. If there isn't it won't match This is a better regex /([-|+])?\s*\d+\.\d{2}/ If it has to have the +/- before it remove the ? after the ) Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062056 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 It will still match | (vertical bar) as a valid sign. Also, you don't need parentheses around that character class. Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062057 Share on other sites More sharing options...
JAY6390 Posted May 22, 2010 Share Posted May 22, 2010 -= EDIT =- Indeed - its getting far too late for me to be reading code Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062068 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 [abc]? will work just fine See: daniel@daniel-laptop:~$ cat test.php <?php $pattern = '/^[abc]?$/'; var_dump(preg_match($pattern, 'a')); var_dump(preg_match($pattern, '')); var_dump(preg_match($pattern, 'hello')); ?> daniel@daniel-laptop:~$ php test.php int(1) int(1) int(0) Quantifiers just operate on the previous subpattern and a character class qualifies as a subpattern. Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062069 Share on other sites More sharing options...
JAY6390 Posted May 22, 2010 Share Posted May 22, 2010 ^^ I edited my post after I pulled my head outta my ass Quote Link to comment https://forums.phpfreaks.com/topic/202609-need-help-with-preg_match_all-matching-numbers-with-decimals/#findComment-1062070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.