Jump to content

[SOLVED] numbers separated by ,


linkin

Recommended Posts

<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>

<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.

<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

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

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

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>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.