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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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