I'm trying to finish the last part of this code, but can't seem to get the last 2 if statements to be valid when they should. The second one should display when there is no letters numbers or dashes(-). The third should make sure the 2 dashes are non consecutive and not at the beginning or end. The expressions have to be wrong, but I have no idea on how to correct them still new to regex. Thanks for your help in advance! Here's what I have so far...
import java.util.Scanner;
public class VerifySerialBHarris {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String text;
String cha;
System.out.println("Enter a Serial number: ");
text=input.nextLine();
if(text.matches("[A-Za-z0-9]+(-[A-Za-z0-9]+){2}")){
System.out.println("Serial number "+text+" verification \nValid");
System.out.println("Enter a wildchar character: ");
cha=input.nextLine();
text= text.replaceAll("[A-Za-z0-9]", cha);
System.out.println("Masked serial: "+text);
}
if(text.matches(".[^-A-Za-z0-9].")) {
System.out.println("Invalid. Serial should only contain letters, numbers, and dashes(-)");
}
if(text.matches("[^A-Za-z0-9]+(-[A-Za-z0-9]+)"))
System.out.println("Invalid. There should be exactly 2 non-consecutive dashes in the middle. ");
}
}