6

What would be a regular expression that would evaluate to true if the string has one or more letters anywhere in it.

For example:

1222a3999 would be true

a222aZaa would be true

aaaAaaaa would be true

but:

1111112())-- would be false

I tried: ^[a-zA-Z]+$ and [a-zA-Z]+ but neither work when there are any numbers and other characters in the string.

Alan Moore
  • 68,531
  • 11
  • 88
  • 149
codenamepenryn
  • 431
  • 1
  • 7
  • 18
  • `.*[a-zA-Z}.*`, assuming your string does not contain new lines – amit Apr 10 '14 at 22:34
  • It should work even if the string does contain new lines. – Barmar Apr 10 '14 at 22:35
  • Check out [my answer](http://stackoverflow.com/questions/22997855/regex-match-if-string-has-no-alphanumic-characters/22998120#22998120) in [this question](http://stackoverflow.com/questions/22997855/regex-match-if-string-has-no-alphanumic-characters) – aliteralmind Apr 10 '14 at 22:38

4 Answers4

10

.*[a-zA-Z].*

The above means one letter, and before/after it - anything is fine.

In java:

String regex = ".*[a-zA-Z].*";
System.out.println("1222a3999".matches(regex));
System.out.println("a222aZaa ".matches(regex));
System.out.println("aaaAaaaa ".matches(regex));
System.out.println("1111112())-- ".matches(regex));

Will provide:

true
true
true
false

as expected

amit
  • 166,614
  • 24
  • 210
  • 314
2

This regexp should do it:

[a-zA-Z]

It matches as long as there's a single letter anywhere in the string, it doesn't care about any of the other characters.

[a-zA-Z]+

should have worked as well, I don't know why it didn't for you.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Well, at least in java terminology - it does not *match*, it *contains* the stated regex. – amit Apr 10 '14 at 22:35
  • Because `match` usually checks the entire string. – Qix - MONICA WAS MISTREATED Apr 10 '14 at 22:35
  • Yeah, I'm using NSPredicate (for iOS), I think it has to match the entire string, not just contain it. – codenamepenryn Apr 10 '14 at 22:35
  • 1
    If you need an answer specific to a particular language, you should say that in the question, and tag the question with the language. – Barmar Apr 10 '14 at 22:36
  • I don't now Java, but it looks to me like you can make a `Matcher` from the regexp, and then use its `find()` method to find the pattern anywhere in the input. – Barmar Apr 10 '14 at 22:41
  • I'm pretty sure Java and NSPredicate (a class in the iOS/Cocoa Objective-C framework) are mutually exclusive. That's why I deleted the NSPredicate tag. @codenamepenryn, what exactly are you using? – Alan Moore Apr 10 '14 at 22:49
  • I'm using NSPredicate, but it's OK. One of the answers worked and I accepted it already. – codenamepenryn Apr 11 '14 at 18:12
2

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

Depending on the implementation, match() functions check if the entire string matches (which is probably why your [a-zA-Z] or [a-zA-Z]+ patterns didn't work).

Either use match() with the above pattern or use some sort of search() method instead.

Qix - MONICA WAS MISTREATED
  • 12,202
  • 13
  • 73
  • 131
0

.*[a-zA-Z]?.*

Should get you the result you want.

The period matches any character except new line, the asterisk says this should exist zero or more times. Then the pattern [a-zA-Z]? says give me at least one character that is in the brackets because of the use of the question mark. Finally the ending .* says that the alphabet characters can be followed by zero or more characters of any type.

Darren
  • 1
  • 1