3

I am doing validation of String which contains ASAHSHAS,342746726,GHG55656 this valid String and spaces are allowed. Thanks In advance!!

skaffman
  • 398,947
  • 96
  • 818
  • 769
vinod
  • 1,178
  • 4
  • 16
  • 42
  • You said it **must** begin with letter, how can 342746726 be valid? – adarshr Mar 15 '11 at 16:57
  • @adarshr I think he means that the entire thing is a string (including the commas). At least that's what I think he means. – Vivin Paliath Mar 15 '11 at 16:59
  • @adarshr you are correct i am wrong that string must start with letters only – vinod Mar 15 '11 at 17:05
  • @adarshr, it appears that the OP is just trying to ask the same question again, instead of improving the original: http://stackoverflow.com/questions/5314777/need-a-code-using-regular-expression-in-java-string-must-be-captial-lettersa – jzd Mar 15 '11 at 17:21
  • @jzd - Yes indeed you're right! I too got the same doubt but didn't bother to verify. Voting to close now. – adarshr Mar 15 '11 at 17:28

2 Answers2

3

This should do it.

^[a-zA-Z][a-zA-Z0-9 ]+$
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • Am I the only one that worries that a-zA-Z may not match all letters? It's Unicode, so what about Greek, Cyrillic, and other alphabets (see http://www.unicode.org/Public/3.0-Update/Blocks-3.txt). – The Archetypal Paul Mar 15 '11 at 17:03
  • it's just contain A-Z and 0-9 and spaces and must start with letter – vinod Mar 15 '11 at 17:07
0

The regular expression that you are looking for may look like this ^\\w[\\w\\d ,]*$. This is an example using it:

String string = "ASAHSHAS,342746726,GHG55656";
if (string.matches("^\\w[\\w\\d ,]*$")) {
    System.out.println("It matches!");
}
else {
    System.out.println("It does NOT match!");
}
redent84
  • 18,901
  • 4
  • 62
  • 85