0

I have never worked with RegEx and have been trying to perform validation to ensure a module code matches the correct format. A valid module code should be in the form: CSC8001

My code is as follows:

if(moduleCode.matches("^CSC8\d{3}")){ 
        throw new IllegalArgumentException();
    }

This produces an invalid escape sequence error which I have been unable to resolve.

Thanks in advance, Mark.

1 Answers1

1

You must use:

moduleCode.matches("^CSC8\\d{3}")

\d is an illegal character. To make it \d you must use \\d.

\\ escapes to form a single back slash.

dryairship
  • 6,022
  • 4
  • 28
  • 54