Validating strings containing alphanumerics & spaces[Sol

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Validating strings containing alphanumerics & spaces[Sol

Post 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]+$";

Code: Select all

test string:
asdf 32
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?
Last edited by raghavan20 on Mon Nov 07, 2005 7:10 am, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

<?php
$test = 'asdf 32';
echo preg_match('/^[a-z0-9]+[a-z0-9 ]*$/i', $test) ? 'Match' : 'Failed';
?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

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

Code: Select all

/^[a-z0-9]+(?: ?[a-z0-9])*$/i'
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 :cry:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :oops:

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

?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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!';
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Code: Select all

[[:space:]]
Yuck.... I don't really like POSIX at all :(
Post Reply