Thursday, 15 September 2011

Regex for letters plus space -



Regex for letters plus space -

i'm trying utilize regex testing letters , spaces allowed in input field. tried using /^[a-za-z]*$/ doesn't allow spaces.

updated question. i'm looking have 1 space between words. illustration have city name new orleans letters , 1 space between words should allowed.

the [a-aa-z] character class, , \s character classes have next caveats.

a-za-z may restrictive if wish take unicode, in case, \p{alpha} improve character class.

\s may either permissive or restrictive depending on unicode or ascii semantics under regex operating. may want set single space in character class.

if knew language , whether or not intend unicode semantics, might help. things aren't simple in ascii-only days.

update:

here's more robust solution still has original unicode caveats, allows single space character between words, , else.

use strict; utilize warnings; @cities = ( 'new orleans', 'new jersey', 'salt lake city', 'sacramento', ' leading space', 'trailing space ', 'two consecutive spaces', ' 2 leading spaces', 'two trailing spaces ', ); foreach $city ( @cities ) { print "<<$city>>: "; if( $city =~ m/^[a-za-z]+(?:\s[a-za-z]+)*$/ ) { print "match\n"; } else { print "reject\n"; } }

regex

No comments:

Post a Comment