1

Objective:match exact 10 of mixed uppercase characters (except I,O, and space) and numeric from 2 to 9:

To be exact, here is the valid set the value must be coming from:

{'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K','L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

I have tried this:

^[A-Z2-9][^IO01a-z ]{10}$

But this 11 length would pass: 9PAUUH98TYE while 10 of them wouldn't pass 9PAUUH98TY

I also tried:

\b[A-Z2-9][^IO01a-z ]{10}\b
4
  • Do you need to use regex for this? Commented Oct 8, 2020 at 3:17
  • @gunr2171 thanks for prompt response and yes as it will be used in <asp:RegularExpressionValidator> Commented Oct 8, 2020 at 3:20
  • Are you expecting your input string to be (when it is valid) 10 characters, or will it be a longer entry that contains the 10 characters you are checking for? Commented Oct 8, 2020 at 3:23
  • @gunr2171 I'm expecting the input string is exactly 10 characters, no more no less and must be from the valid set of listed characters (which is all upper case excluding I, O and space, or any symbol, and numeric from 2 to 9. Commented Oct 8, 2020 at 3:28

3 Answers 3

3

So you could do some fancy negative lookaround magic, but really, just listing out the letters you expect is fine.

^[ABCDEFGHJKLMNPQRSTUVWXYZ2-9]{10}$

Using spanning, it can be shortened to

^[A-HJ-NP-Z2-9]{10}$

This is the """lazy""" method, but it will work consistently the same across all regex flavors. Negative lookbehinds sometimes might behave differently in .Net vs JavaScript vs others, for example.

Also, it's SUPER important to use ^ and $ in your pattern. They will help to ensure that the whole input string is the correct number of characters.

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

3 Comments

It might look dumb but for most of the cases, this is a more consistent and straightforward solution.
Good, because I'm a dumb but straightforward guy :) (and BTW I do want your answer to get the accept, I do like it better)
Lol, no offense. That was supposed to be a compliment but in a tsundere way.
1

You can use negative lookahead/lookbehind to exclude the set you don't want:

\b([A-Z2-9](?<![IO])){10}\b

Check here to see the test cases

Comments

1

Would this work?

\b[A-HJ-NP-Z2-9]{10}\b

Demo

2 Comments

Honestly, this is a little bit confusing that if you're not very familiar with the alphabet, you won't tell which letters are missing...
@HaoWu it is indeed confusing as I did miss the I and O at first, until double checked later to find out certain letters were purposedly left out.

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.