0

Regex:

^[U][S][A]\d{2}[C][S]\d{3}

It indicates these values are valid:

USA25CS131 (valid)
USA25CS1311 (invalid length)

How do I get it to reject a string that's too long?

1
  • [U] is equal to U. So either have [USA]{3} which can be anything from SSS to UUU or just type USA. Commented Oct 8, 2020 at 17:00

2 Answers 2

5

You can simply anchor the regex at the end with $, like this:

^[U][S][A]\d{2}[C][S]\d{3}$

and now you won't match strings with extra characters at the end.

You can also simplify your regex to this:

^USA\d{2}CS\d{3}$
Sign up to request clarification or add additional context in comments.

Comments

1
^\w{1,10}$

That will match on anything 1-10 characters long, ^ means start of the line and $ means end of the line. You can adapt this with your code to produce the results you want.

Given your specific regex I would use ^[U][S][A]\d{2}[C][S]\d{3}$(just adding $ to the end)

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.