"[a-zA-Z]"仅匹配一个字符。要匹配多个字符,请使用
"[a-zA-Z]+"。
由于点对于任何角色都是小丑,因此您必须屏蔽它:
"abc."要使点成为可选,您需要一个问号:
"abc.?"
如果在代码中将Pattern作为文字常量编写,则必须屏蔽反斜杠:
System.out.println ("abc".matches ("abc\.?"));System.out.println ("abc.".matches ("abc\.?"));System.out.println ("abc..".matches ("abc\.?"));结合两种模式:
System.out.println ("abc.".matches ("[a-zA-Z]+\.?"));w通常比a-zA-Z更合适,因为它捕获äöüßø等外来字符:
System.out.println ("abc.".matches ("\w+\.?"));


