Strange regex behavior? Multiple spaces with \s*

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

Moderator: General Moderators

Post Reply
anthonylebrun
Forum Newbie
Posts: 3
Joined: Sun May 03, 2009 10:20 am

Strange regex behavior? Multiple spaces with \s*

Post by anthonylebrun »

The following validates one user data entry:

Code: Select all

 
if (!filter_var($_POST['name'], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/\s*/")))) {
  $errmsg['name'] = "<font size='1' color='red'>Everyone has a name...</font>";
  $valid_form = FALSE;
}
 
Full code here: http://snipt.org/Wgj
Test it here: http://pierre-tori.com/sandbox/form.php

I mean for it to issue a warning when the user inputs nothing or whitespace.
However the regex

Code: Select all

/\s*/
only matches a completely blank input field.

What gives?
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: Strange regex behavior? Multiple spaces with \s*

Post by jazz090 »

you are using asterisk which suggest that whitespace may not be present: use /\s+/ instead (this validates whitespace) to validate a void entry use the empty() function.
anthonylebrun
Forum Newbie
Posts: 3
Joined: Sun May 03, 2009 10:20 am

Re: Strange regex behavior? Multiple spaces with \s*

Post by anthonylebrun »

jazz090 wrote:you are using asterisk which suggest that whitespace may not be present: use /\s+/ instead (this validates whitespace) to validate a void entry use the empty() function.
Shouldn't /\s*/ cover those cases (empty, single and multiple spaces)? I tried your suggestion but the program is still exhibiting the same behavior as before.

Code: Select all

if (empty($_POST['name']) || !filter_var($_POST['name'], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/\s+/")))) {
Sooo.. what gives? :?:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Strange regex behavior? Multiple spaces with \s*

Post by prometheuzz »

anthonylebrun wrote:...

Code: Select all

/\s*/
only matches a completely blank input field.

What gives?
No, that regex wil match any string. Because any string has "zero or more white space characters" in it. You'll probably want to "anchor" the start- and end-of-line in your regex:

Code: Select all

/^\s*$/
The regex above will match an empty string and strings containing white space characters only.
anthonylebrun
Forum Newbie
Posts: 3
Joined: Sun May 03, 2009 10:20 am

Re: Strange regex behavior? Multiple spaces with \s*

Post by anthonylebrun »

Oh that makes sense, thanks!

EDIT: for some reason it still doesn't match a completely blank entry (as obtained from $_POST) however adding

Code: Select all

empty($_POST['name']) ||
solved that. Anyone know why this is?
Last edited by anthonylebrun on Mon May 04, 2009 6:11 am, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Strange regex behavior? Multiple spaces with \s*

Post by prometheuzz »

No problem.
Post Reply