我想在while循環中使用正則表達式驗證用戶輸入。我只想接受一個小寫字母(包括字母a-z)。在Java中的正則表達式:驗證用戶輸入
public class testRedex{
public static void main(String [] args){
Scanner console = new Scanner(System.in);
System.out.print("Please enter a key: ");
while(!console.hasNext("[a-z]+")){
System.out.println("Invalid key");
console.next();
}
String test = console.next();
System.out.println(test);
}
}
我的問題是,是否有具有正則表達式爲 「[A-Z] +」 和 「[A-Z] + $」 之間的任何差異?我知道$會在字符串末尾的a-z之間尋找一個字符,但是在這種情況下它會起作用嗎?
給接受輸入的樣本。 –
一些例子是像「貓,魚,狗,sjakjsdljasmx,五,regularexpresson等」。 – user3808889