-2

My code cannot enter dot. Can someone help me?

public boolean isValidLastName(final String lastname){
    Pattern pattern;
    Matcher matcher;
    final String LASTNAME_PATTERN = "[a-zA-Z]+\\.?";
    pattern = Pattern.compile(LASTNAME_PATTERN);
    matcher = pattern.matcher(lasname);
    return matcher.matches();
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Adda
  • 81
  • 1
  • 8

3 Answers3

0

Dot should be placed inside brackets. Use this:

[ .a-zA-Z]+

You may also try \w\d in the brackets to replace , but I’m not sure if Java supports it.

phye
  • 49
  • 1
  • 5
-1

Try using this one instead [a-zA-Z.\s]+ - allowing alphabet or dot or whitespaces.

20 fps
  • 342
  • 5
  • 18
-1

You shouldnt use double "\" like in java's String before special characters, instead just put dot in brackets. You can always check how your regular expression works https://www.regextester.com You can read more about regex Patterns in doc:https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

Stile
  • 31
  • 6