1

I want to validate a string of variable length - a VIN number. I want to ensure that it contains only alphanumeric characters and is between 9 and 15 digits in length. However, I also want to make sure that nowhere in the string is a sequence of six or more alphanumeric characters. I am using .NET's regex engine.

00THZY7564TTX --> should pass validation
X0000000TEVBV --> should fail validation

I've looked at a number of posts, but I can't seem to get it down 100%. Currently, I have

(?<!(0){5,}.*)(?!.*(0){5,}.*)[A-Za-z0-9]{1,}

but it seems to ignore the zeros in the string (in example #2 above, it matches the 0000TEVBV portion).

What am I doing wrong?

Thanks in advance!!

zx81
  • 38,175
  • 8
  • 76
  • 97
user1422348
  • 345
  • 7
  • 20

1 Answers1

1

Use this regex:

^(?!.*(\d)\1{5})[A-Z0-9]{9,15}$

See the matches in the regex demo.

  • Allows 9 to 15 upper-case letters or digits with [A-Z0-9]{9,15}
  • Does not allow one character (e.g. A) to be in a six-sequence, e.g. AAAAAA
  • The ^ anchor asserts that we are at the beginning of the string
  • The $ anchor asserts that we are at the end of the string
zx81
  • 38,175
  • 8
  • 76
  • 97
  • FYI made an assumption about what you meant by `a sequence of six or more alphanumeric characters`—let me know if you need any tweaks. :) – zx81 Jul 23 '14 at 23:56
  • Thanks, glad it helped. :) – zx81 Jul 24 '14 at 00:02
  • I'm not 100% sure, but I think that .net XSD regex doesn't use lookaheads. Is there any way to solve this without using lookarounds? – user1422348 Jul 25 '14 at 01:51
  • Mmm if it's .NET regex then lookaheads are definitely supported. – zx81 Jul 25 '14 at 02:02
  • They are usually, but when using it inside an XSD document, it isn't working. And according to John's answer here http://social.msdn.microsoft.com/Forums/en-US/8b7aa4e3-7d84-4a0d-9974-cd1e0769dbc0/problem-validating-xml-against-xsd-with-regex-as-string-pattern?forum=regexp, it doesn't look like the lookaround feature is supported. – user1422348 Jul 25 '14 at 02:05
  • The only other option I can think of is to use an alternation to match what you **DON'T** want in order to neutralize it, and to mach **AND CAPTURE** what you **DO** want to Group 1. If the group is set, you have a match. `^(?:.*(\d)\1{5}.*|([A-Z0-9]{9,15}))$` For more on this technique read [my answer here](http://stackoverflow.com/q/23589174/1078583). Whether your syntax allows you to examine Group 1, I don't know. :) – zx81 Jul 25 '14 at 03:43