Results 1 to 3 of 3

Thread: [RESOLVED] Help with regular expression

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Resolved [RESOLVED] Help with regular expression

    I need to test a string of text that needs to contain a certain type of char values. The known is allowing of a-z,A-Z,0-9. But the variable (which will be coming from a database field) will be the allowed symbols. The allowed symbols will look something like:

    !@#$%^&*()

    So using regex, how can I check a string of text to make sure it ONLY contains a-z,A-Z,0-9 AND the allowed symbols as shown above? Remember, the allowed symbols is dynamic at run time so I need to somehow be able to plug that in to the method at run time.

    The following examples would be valid using the above symbol string:

    Test1234%
    Test1234

    The following example would NOT be valid:

    Test1234%_

    Visual Studio 2010

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Help with regular expression

    Code:
    Dim pattern = "A-Za-z0-9"
    pattern &= Regex.Escape("!@#$%^&*()")
    pattern = "^[" & pattern & "]+$"
    
    Dim exp As New Regex(pattern)
    
    Debug.WriteLine(exp.IsMatch("Test1234%"))
    Debug.WriteLine(exp.IsMatch("Test1234"))
    Debug.WriteLine(exp.IsMatch("Test1234%_"))
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] Help with regular expression

    Perfect, thanks!

    Visual Studio 2010

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