CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    28

    Regex code not working

    Hi,

    I have written a short piece of code to validate a UK NI number.

    The format is 2 upper case letters, followed by 6 digits followed by A,B, C or D

    eg AB123456A is OK

    This code does not work, it never matches even when the string NI_Num is OK:


    Dim NIr As Regex
    Dim match As Boolean

    NIr = New Regex("^[A-Z]^2[0-9]^6[A,B,C,D]")

    match = NIr.IsMatch(NI_Num(count))


    match is always false. Any suggestions where I'm going wrong?

    Tia

    H
    Last edited by happyme; April 2nd, 2009 at 08:52 AM.
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Regex code not working

    I just did a search on RegEx Lib and they have posted a Regular Expression for validating UK National Identification numbers here:
    http://regexlib.com/REDetails.aspx?regexp_id=527

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Regex code not working

    Quote Originally Posted by happyme View Post
    Hi,

    I have written a short piece of code to validate a UK NI number.

    The format is 2 upper case letters, followed by 6 digits followed by A,B, C or D

    eg AB123456A is OK

    ...
    Code:
    Dim NIr As Regex
                    Dim match As Boolean
    
                    NIr = New Regex("^[A-Z]^2[0-9]^6[A,B,C,D]")
    
                    match = NIr.IsMatch(NI_Num(count))

    match is always false. Any suggestions where I'm going wrong?

    Tia

    H
    "^" means "begining of line or string".

    Try Expresso software to understand and debug regular expressions.

    By the way, you probably want this:
    Code:
    "^[A-Z]{2}[0-9]{6}[A,B,C,D]"
    [Vb.NET 2008 (ex Express)]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured