regexp letters+spaces

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

Moderator: General Moderators

Post Reply
Qlubbie
Forum Newbie
Posts: 1
Joined: Mon Jul 02, 2007 8:22 am

regexp letters+spaces

Post by Qlubbie »

Ok i want to check an input field on letters and spaces and a space must be followed by a letter...
^([a-z]+)$ checks on letters... but what am i supposed to add to allow a space?

Thanx in advance
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Add \s
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

^[\w]+\s[\w]+
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\w matches far more than simply letters.

Code: Select all

[feyd@home]>php -r "$t = array('last word should pass', 'last word should fail '); foreach($t as $a) { preg_match_all('#^[a-z]+(?:\s[a-z]+)*$#mi', $a, $matches); print_r($matches); }"
Array
(
    [0] => Array
        (
            [0] => last word should pass
        )

)
Array
(
    [0] => Array
        (
        )

)
Is there an actual problem with spaces though? trim() should probably be used anyways..
Post Reply