2

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

Example

3 Answers3

0

Match the serial exactly

^[A-Za-z0-9]{4}\x2d[A-Za-z0-9]{4}\x2d[A-Za-z0-9]{4}

Use Regexr.com to build and test

Dylanrr
  • 31
  • 1
  • 6
0

You already have the full pattern [A-Za-z0-9]+(-[A-Za-z0-9]+){2} and it might be easier to list the pattern and to give an error message stating that the entered data does not adhere to the required pattern.

If you want to give feedback to the user by analyzing the kind of error that was made, then you have to create a pattern for all the kind of mistakes a user can make. For the code I suggest making use of for example an if else if structure so that you don't get the error message while the pattern is correct.

About the second and the third patterns

The second one should display when there is no letters numbers or dashes(-).

The second pattern .[^-A-Za-z0-9]. will match 3 characters where the first and the last can be any character except a newline due to the dot and can also match a letter or number.

To match no letters numbers or dashes, you could use:

.*[^-A-Za-z0-9].*

The third should make sure the 2 dashes are non consecutive and not at the beginning or end

The third pattern [^A-Za-z0-9]+(-[A-Za-z0-9]+) first matches what is not in the character class due to the negation using [^ can also match a hypen because it is not listed but does not have to.

To match that pattern you could repeat the hyphen 1+ times:

[A-Za-z0-9]+(-+[A-Za-z0-9]+){2,}

Updated Java code

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Your first regex, [A-Za-z0-9]+(-[A-Za-z0-9]+){2}, already matches only codes that contain three alphanumeric blocks with exactly two non-consecutive interstitial dashes.

Where you are using the second regex, it's mostly problematic because you've got your condition inverted. It's easier to ask whether it FAILS to match [-A-Za-z0-9]+ than that it DOES match some other pattern which only fits sequences with invalid characters -- a confusing double negative.

For the third condition, you probably don't need to check anything at all. You've already eliminated all the other failure cases (invalid characters and incorrect layout) so you can just describe that you need 3 alphanumeric blocks separated by a single dash.

And as it's been mentioned elsewhere, you should be using if ... else if ... else ... etc.

Mumbleskates
  • 1,248
  • 10
  • 18