Page 1 of 1
Validating strings containing alphanumerics & spaces[Sol
Posted: Sat Nov 05, 2005 2:11 pm
by raghavan20
I want a regex expression that can allow alphanumerics and space between words.
Code: Select all
regex used:
regex = "^[a-zA-Z0-9]+\s?[a-zA-Z0-9]+$";
But the above regex fails in this case...I am not able to figure out why...
The above works fine without ^ and $...why is that?
Posted: Sat Nov 05, 2005 2:34 pm
by redmonkey
Code: Select all
<?php
$test = 'asdf 32';
echo preg_match('/^[a-z0-9]+[a-z0-9 ]*$/i', $test) ? 'Match' : 'Failed';
?>
Posted: Sun Nov 06, 2005 5:39 pm
by raghavan20
Thanks for your reply redmonkey
but yours will allow string like that end with a trailing space
like
Code: Select all
asdf <---here is a space after asdf
but I want to allow words separated by spaces...this regex worked
Code: Select all
"^[a-z0-9]+([a-z0-9 ]*[a-z0-9])*$"
1. How does it allow just space to denote a space inside the regex
I mean...
Code: Select all
"[a-zA-Z ]" <---------The space after capital 'Z'; is it not possible to use \s?
But I still don't know why this regex does not work. What is the most apparent error you can see
Code: Select all
"^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$"
does not work true on:
asdf 32
Posted: Sun Nov 06, 2005 6:00 pm
by redmonkey
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.
\s matches any white space character so it could match a space or tab etc.
Not tested but you could try..
Posted: Sun Nov 06, 2005 6:38 pm
by Chris Corbyn
Code: Select all
/^[a-z0-9]+[a-z0-9\s]*?[a-z0-9]$/i
Be specific about the *individual* character at the start and end of the string. The middle just uses a charatcer class that includes the space (ungreedy).
Posted: Mon Nov 07, 2005 3:58 am
by raghavan20
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
I am really sorry that I still do not find the problem you pointed out.
Rules for that regex:
===================
1. The start of the string should be alphanumerics repeating more than once
2. If a space is included, then it should be followed by alphanumerics more than once and this is optional
Compare with the input asdf 32:
1. asdf ----starts with alphanumerics
2. 32 - there is a space and followed by alphanumerics more than once
I don't see a problem here

Posted: Mon Nov 07, 2005 4:01 am
by Chris Corbyn
It's looking for something more like "adsf 3 2 a d s f"
Put a * on the "\s"
EDIT | No that works anyway.... it's the "\s" followed by multiple aplahnumerics so yes it's OK
How are you using it?
Code: Select all
<?php
if (preg_match('/^[a-zA-Z0-9]+(\s[a-zA-Z0-9]+)*$/', 'asdf 32')) echo 'Foo!';
?>
Posted: Mon Nov 07, 2005 4:09 am
by raghavan20
It works with preg_match but not with ereg
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!';
Posted: Mon Nov 07, 2005 4:37 am
by Chris Corbyn
raghavan20 wrote:It works with preg_match but not with ereg
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 powerful

Posted: Mon Nov 07, 2005 5:42 am
by raghavan20
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?
Posted: Mon Nov 07, 2005 5:51 am
by Chris Corbyn
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?
ereg_...() is
not PHP's.
ereg is POSIX regex.... preg_...() is Perl compatible regex.
In POSIX \s ia not whitespace.... that's Perl...
Yuck.... I don't really like POSIX at all
