tags:

views:

56

answers:

3

Hi, i have this expression ([a-zA-Z]|ñ|Ñ)* which i want to use to block all characters but letters and Ñ to be entered on a textbox. The problem is that return a match for: A9023 but also for 32""". How can i do to return a match for A9023 but not for 32""". Thanks.

A: 

Sure that you don't mean ^([a-zA-Z]|ñ|Ñ)*$ -- you might be finding the characters you want but not excluding what you don't? The expression I mentioned will pin to the beginning ^ and the end $ of the string, so that nothing else will pass. Otherwise:

123ABC456

...will pass your match, because it found 0-or-more letters... though there were also other letters.

eruciform
+4  A: 

You need to add assertions for the start and the end of the string:

^([a-zA-Z]|ñ|Ñ)*$

Otherwise the regular expression matches at any position. Additionally, you can also write ([a-zA-Z]|ñ|Ñ)* as the character class [a-zA-ZñÑ]*:

^[a-zA-ZñÑ]*$
Gumbo
also, Expresso is a great tool for testing regex... http://www.ultrapico.com/Expresso.htm
rockinthesixstring
A: 

You didn't say which regex flavor (which programming language) you're using, but you might want to consider either

^\p{L}*$

if your regex flavor supports Unicode properties or

^[^\W\d_]*$

if it doesn't.

Reason: Your regex will allow only unaccented letters and Ñ - is there a real language that uses the latter without also having accented letters?

\p{L} means "any letter in any 'language'",

[^\W\d_] means "any character that is neither a non-alphanumeric, a digit or an underscore", which is just a fancy but necessary way to say "any letter" (\w is a shorthand for "letter, digit or underscore", \W is the inverse of that).

Tim Pietzcker