linkin Posted August 14, 2008 Share Posted August 14, 2008 Can someone please show me the regex code for validating a string composed of numbers only and separated by this character , e.g. 1,2,3,4,5,6,7 21,532,123,7665,12,34,23,6,3 thank you Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 <pre> <?php $tests = array( '1,2', 'a,b,c', ',1,2,3', '2,10,10000,9,0', ',', ',9', '10,', '1,a,3', '1,,100', '1,2,3,4,5,6,7', '21,532,123,7665,12,34,23,6,3' ); foreach ($tests as $test) { echo "$test — "; echo preg_match('/\A(?:\d+,)+\d+\z/', $test) ? 'Valid' : 'Invalid' ; echo '<br>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
linkin Posted August 14, 2008 Author Share Posted August 14, 2008 <pre> <?php $tests = array( '1,2', 'a,b,c', ',1,2,3', '2,10,10000,9,0', ',', ',9', '10,', '1,a,3', '1,,100', '1,2,3,4,5,6,7', '21,532,123,7665,12,34,23,6,3' ); foreach ($tests as $test) { echo "$test — "; echo preg_match('/\A(?:\d+,)+\d+\z/', $test) ? 'Valid' : 'Invalid' ; echo '<br>'; } ?> </pre> That's just fantastic....a big thank you. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 <pre> <?php $tests = array( '1,2', 'a,b,c', ',1,2,3', '2,10,10000,9,0', ',', ',9', '10,', '1,a,3', '1,,100', '1,2,3,4,5,6,7', '21,532,123,7665,12,34,23,6,3' ); foreach ($tests as $test) { echo "$test — "; echo preg_match('/\A(?:\d+,)+\d+\z/', $test) ? 'Valid' : 'Invalid' ; echo '<br>'; } ?> </pre> Hi effigy.. I'm just rather curious.. is there a need for the non-capture ' (?: ' in that preg? Doesn't that mean a non-capture for backreferencing purposes (in otherwords, backreferencing will not include that)? If so (as in, if I got that right) since there is no backreferencing, would it be necessary? Cheers, NRG Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 The short answer: We don't need to capture anything. They're required for the quantifier. The long answer: "[G]rouping-only parentheses... don't capture, but just group regex components for alternation and the application of quantifiers. .... They can also be efficient -- if the regex engine doesn't need to keep track of the text matched for capturing purposes, it can work faster and use less memory. .... Non-capturing parentheses are useful when building up a regex from parts." -- Mastering Regular Expressions Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 Ah I see.. so its just for faster performance / reduction in memory usage. This makes sense now. I thought you were using it because of some backreferencing.. but was confused as there is none.. but now I understand.. so long as the regex engine doesn't need to keep track of matches for capturing, it's faster to add ' ?: ' to the mix. Thanks NRG Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 Well, yes, but that reasoning is secondary. The main reason is so the + quantifier will consider \d+, as the unit to repeat. The only other way would be (\d+,)+ which then leads to the secondary reasoning of "but we don't need to capture this." Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 Ok, I understand (man, I have so much to learn with regex... it's such a deep subject for something with such a simple collection of syntax..). Quote Link to comment Share on other sites More sharing options...
linkin Posted August 15, 2008 Author Share Posted August 15, 2008 I just tweaked your code a little to allow a single number as well, this code is important when validating multiple input into mysql i.e. when you are using the IN statement in sql. $tests = array( '1,2', 'a,b,c', ',1,2,3', '2,10,10000,9,0', ',', ',9', '10,', '5', '1,a,3', '1,,100', '1,2,3,4,5,6,7', '21,532,123,7665,12,34,23,6,3' ); foreach ($tests as $test) { echo "$test — "; echo preg_match('/\A(?:\d+,|)+\d+\z/', $test) ; echo '<br>'; } Quote Link to comment Share on other sites More sharing options...
effigy Posted August 18, 2008 Share Posted August 18, 2008 The same thing can be achieved by changing the quantifier: /\A(?:\d+,)*\d+\z/. Quote Link to comment 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.