Code: Select all
regex used:
regex = "^[a-zA-Z0-9]+\s?[a-zA-Z0-9]+$";Code: Select all
test string:
asdf 32The above works fine without ^ and $...why is that?
Moderator: General Moderators
Code: Select all
regex used:
regex = "^[a-zA-Z0-9]+\s?[a-zA-Z0-9]+$";Code: Select all
test string:
asdf 32Code: Select all
<?php
$test = 'asdf 32';
echo preg_match('/^[a-z0-9]+[a-z0-9 ]*$/i', $test) ? 'Match' : 'Failed';
?>Code: Select all
asdf <---here is a space after asdfCode: Select all
"^[a-z0-9]+([a-z0-9 ]*[a-z0-9])*$"Code: Select all
"[a-zA-Z ]" <---------The space after capital 'Z'; is it not possible to use \s?Code: Select all
"^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$"
does not work true on:
asdf 32Code: Select all
/^[a-z0-9]+(?: ?[a-z0-9])*$/i'Code: Select all
/^[a-z0-9]+[a-z0-9\s]*?[a-z0-9]$/iI am really sorry that I still do not find the problem you pointed out.redmonkey wrote: string:
asdf 32
regex:
"^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$"
The apparent error is that your second part of your pattern is looking for a white space character followed by any alphanumeric repeated to the end. Therefore ' 32' will not match
Code: Select all
<?php
if (preg_match('/^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$/', 'asdf 32')) echo 'Foo!';
?>Code: Select all
if (preg_match('/^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$/', 'asdf 32')) echo 'preg_match!';
if (ereg("^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$", 'asdf 32')) echo 'ereg!';Two different regex engines.... why would you prefer ereg? It's slower and less powerfulraghavan20 wrote:It works with preg_match but not with eregCode: Select all
if (preg_match('/^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$/', 'asdf 32')) echo 'preg_match!'; if (ereg("^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$", 'asdf 32')) echo 'ereg!';
ereg_...() is not PHP's.raghavan20 wrote:I don't really prefer ereg, I normally use preg_match and preg_replace...I thought this time I should use ereg as it is PHP rather using Perl's preg...but I studied from the documentation that you told "Perl regex is powerful and faster than PHP regex". I have to change now.
But did you find why ereg does not work in that case?
Code: Select all
[[:space:]]