0

I am trying to validate user input using a regular expression in a while loop. I am trying to accept only one lower-case word (letters a-z inclusive).

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);          
    }
}

My question is, is there any difference between having the regular expression as "[a-z]+" and "[a-z]+$"? I know that $ will look for a character between a-z at the end of the string, but in this case would it matter?

2
  • give a sample of accepted input. Commented Jul 6, 2014 at 4:55
  • Some examples would be words like "cat, fish, dog, sjakjsdljasmx, five, regularexpresson, etc." Commented Jul 6, 2014 at 4:56

2 Answers 2

2

Yes there is a difference, if you'll use: ^[a-z]+$ it means that whatever the user inputs should be combined only from [a-z]+.

If you don't add the ^ and the $ the user could insert other characters, space for example, and there will still be a match (the first part of the string until the space.

Let's see an example:

  • run your code with the input: "try this" (it'll print "try")
  • now change the regex to ^[a-z]+$ and run with the same input (it'll print "Invalid key").

The way I would re-write it is:

    System.out.print("Please enter a key: ");

    String test = console.nextLine();
    while (!test.matches("^[a-z]+$")) {
        System.out.println("Invalid key");
        test = console.nextLine();
    }
    System.out.println(test);
Sign up to request clarification or add additional context in comments.

2 Comments

@alfasin Okay, thanks for the update. Do you know why the hasNext() method does not work properly with the regular expression "^[a-z]+$"?
@user3808889 it's not that it doesn't work with the regex: in the original version hasNext() was used right after the call to nextLine() (at the end of the loop) and that caused the mess.
0

The difference are as follows

[a-z]+ means a,b,c,...,or z occurs more than one time

[a-z]+$ means a,b,c,...,or z occurs more that one time and must match end of the line

Yet at the end, they give you the same result.

if you want to understand it better try this

([A-Z])([a-z]+$)

It start with a capital letter and end with more than one time small letter

output:

Please enter a key: AAAAaaaa
Invalid key
Aaaaaa
Aaaaaa

Another way you can get the expected result from what you are looking for

code:

    int i = 0;
    String result = "";
    do{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your key:");

    if(input.hasNext("^[a-z]+$")){
        result = input.next();
        i=1;
    }else{
        System.out.println("Invalid key"); 
    }
   }while(i==0);
    System.out.println("the result is " + result);

output:

Enter your key:
HHHh
Invalid key
Enter your key:
Hellllo
Invalid key
Enter your key:
hello
the result is hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.