java - Checking if a specified position in a string contains a letter or a digit -


i wish check whether string contains letter or digit in specified position.

here's problem:

i have input string of length 2. string of form "a1". i'm making validation method these types of strings.

requirements:

  • the letter @ index position 0 has 1 of letters: a, b, c, d, e, f, g, h
  • the number/digit @ index position 1 has 1 of numbers: 1, 2, 3, 4, 5, 6, 7, 8

thanks!

regex

input.matches("^[abcdefgh][12345678]$"); 

edit:

this equivalent to:

input.matches("^[a-h][1-8]$"); 
  • the ^ anchors match-check beginning of input string.
  • the [a-h] says first character in string (after ^) can 1 of characters through h.
  • the [1-8] says second character can 1 through 8.
  • the $ indicates next thing after [1-8] must end of string.

Comments