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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Will this correctly catch the module format? Thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.