String validation

TechnoTone

Well-known member
Joined
Jan 20, 2003
Messages
224
Location
UK - London
I have a string that must conform to a strict format. The format is as follows:

BBBBCCTTXXX

where

BBBB is a four digit character code
CC is a two digit character code
TT is a two digit alphanumeric code
XXX is an optional three digit alphanumeric code

The whole code is either 8 or 11 characters in length, depending on whether the final three XXXs are included.

The first 6 characters must not contain numbers.
The final 2 or 5 characters (depending on whether its an 8 or 11 length string) can contain numbers though not necessarily.

Nowhere should the string contain anything other than alphanumerics.


Is there an easy way of validating this other than processing the string byte by byte manually?
 
I believe you can do this with Regex, also you can do it with substring and Isnumeric. I cant remember the exact term, but i think you could do it with somesort of format mask on the entry field.
 
OK - so Im trying to do this with Regex but not having much success. Here is my code:

Code:
m = System.Text.RegularExpressions.Regex.Match(cellValue, ":c^6((:a^2)|(:a^5))")
When I check m.Success Im getting False when I should be getting True.
 
the Pattern for your Validation would look like this:
"[a-zA-Z]{6}(\w{5}|\w{2})"

you can use it this way:

Code:
Imports System.Text.RegularExpressions

Public Function Validate(ByVal value As String) As Boolean
    Dim myRegEx as new RegEx("[a-zA-Z]{6}(\w{5}|\w{2})")
    If myRegEx.IsMatch(value) AndAlso myRegEx.Match(value).Length = value.Length Then
        Return True
    Else
        Return False
    End If
End Function

thats it...

Andreas
 
Thanks, but I dont understand why it works. Ive checked the documentation and as far as I can tell, my RegEx string should work but it doesnt. I also cant see why \w or {} are working as they are in your RegEx string.
 
well Ive been using regex in .Net for quite a while.. so I was confused about your ":c^6((:a^2)|(:a^5))" Regex-Pattern.. never seen that Syntax.. (maybe some Perl or other background?)

my Pattern does the following:
match exactly 6 ("{6}") characters ("[a-zA-Z]") followed by either 5 alphanumeric characters ("\w{5}") or 3 ("\w{3}") alphanumeric characters... hope this helps a little bit to clear up... ;)

Andreas

[edit]for playing with regex I really can recomment you visiting www.ultrapico.com and download "Expresso".. THE RegularExpression tool (the best Ive seen until now.. and.. no its not from me.. and I dont get money to say this... ;))[/edit]
 
Thanks. Ive just been reading up on MSDN online and saw the documentation explaining your pattern string - its making sense now. I was originally looking in the MSDN library on my PC (January 2004) which is completely different for some reason. Very confusing.


One last thing. Why does the pattern match return True for strings of length 8, 9, 10 and 11? Shouldnt strings of length 9 and 10 fail the pattern match?
 
the pattern matches if theres any character sequence (8 or 11 characters long) in the string (which can be longer) that fulfills the requirements you posted... thats why I used the "myRegEx.Match(value).Length = value.Length"-part to check whether the string contains anything else than the required string... if it does my function returned False -> string not valid....

Andreas
 
Back
Top