tags:

views:

164

answers:

5

I'm trying to determine the regular expression to verify that a string has at least one alphabetic character.

Any help would be appreciated.

Thanks.

+2  A: 

Try this

"^.*[a-zA-Z].*$"

This will work with most regex engines. However it is limited to ASCII alphabetical characters. For international characters we'd need to know the regex engine involved.

JaredPar
I'm sorry this is a 100% correct answer. Going to need an explanation for the down vote.
JaredPar
I can't actually *explain* it, as it wasn't mine, but you've got a pretty inefficient regex there, comparatively. Most regex engines provide a method to find a match anywhere in the string, in which case thinkcube's answer is a better choice. Methods which are anchored to the start of the string still don't need an explicit and the trailing anchor shouldn't ever be necessary, leaving you with `.*[a-zA-Z]`.
Ben Blank
@Ben - yes it was that, and assuming that the alphabet is a-z.
Alnitak
well, it's certainly not 100% correct, and it's mostly for the a-z assumption.
Alnitak
So you down voted me for providing a valid answer to a vague question for performance reasons? Considering the user didn't even bother mentioning a language I think assuming performance implications is a fairly bad idea. Not exactly how I would use a down vote but everyone's entitled to their opinions.
JaredPar
@Alnitak, the user didn't bother to specify anything and you're down voting for performance and lack of a localized answer? Tell me how you would expect me to give a localized regex when the regex implementation isn't specified
JaredPar
@Alnitak — It's sad that you're downvoting people for making different assumptions than you did. The vast majority of the time, "ASCII letters" is what people mean in situations like this. Add to that the fact that locale-aware matching tends to be *very* engine-dependant, and `[a-zA-Z]` becomes the only good, general answer.
Ben Blank
@JaredPar — For what it's worth, I didn't downvote you, and won't. I may not think it's the *best* answer, but it's entirely valid and should work perfectly well based on my understanding of the question.
Ben Blank
downvote reversed, but with reservations. It would be worth explaining the assumptions made.
Alnitak
@Alnitak, appreciated, qualifications added
JaredPar
To provide a more efficient regular expression in this fashion: `^[^a-zA-Z]*[a-zA-Z].*$`.
Gumbo
@All -- huzzah!
gnomed
This was the answer I needed. Many thanks for JaredPar!
goombaloon
+3  A: 

Here it is:

[a-zA-Z]
Carlos A. Ibarra
+1  A: 

The POSIX standard match for alphabetic characters is:

[[:alpha:]]

The .net equivalent is

[\p{L}]

where this is an MS shortcut for Unicode's 5 different "letter" character classes, which are also supported by Java:

[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}]

Note that this should also match any characters that are alphabetic, not just the usual Roman alphabet's "[a-zA-Z]" set, and therefore also matches accented characters, etc.

Alnitak
[[:alpha:]] isn't widely supported. Python, JavaScript, and .NET (just off the top of my head) do not support it. I believe only PCRE and POSIX do.
Ben Blank
A: 

[a-zA-Z] or even [a-z] if you pass the case-insensitive option to your regular expression engine.

Alex Barrett
not sure why this was downvoted, +1
gnomed
+3  A: 

Don't forget that the definition of 'alphabetic character' is not the same all over the world. For instance, in Norway, the correct regex is [a-zA-ZæøåÆØÅ].

codeape