javaregexstringstring-matchingtextmatching

Java String should contain only letters and dot (period)


I would like to write a checker for my string input. I only want my string to have capital letters and ".".

I am thinking about writing code like a.match("^([A-Za-z]+$"); but I don't know how to exclude "." from this statement. How can I do this?


Solution

  • [A-Z.]+ should do the trick. Note, that you also don't want small letters.

    ^ and $ are not required because String.matches operates on the complete String.

    The . requires no escape, because within character classes (the [...] part) a . has no special meaning.