locked
Simple regex expression help RRS feed

  • Question

  • Hi

    I have the characters that I want to disallow within string, this disallow character sets can grow and shrink depending upon the business rule. I need somethign like this.

    dim disallowedCharacter as string = "@#$%^"

    dim accountName as String = "MYACCOUNT"  'VALID

    dim accountName as String = CHECKING@ACCOUNT 'INVALID

    If Regex.IsMatch(accountName,disallowedCharacter) = True

     Exit

    End IF

     

     

    Thursday, April 22, 2010 9:42 PM

Answers

  •  

    dim disallowedCharacter as string = "\@#$%^", I missed \ on the list, is this all I have to do? Add \ on the list?


    The \ means what to follow is "literal". In this case the literal ^

    But you must encapsulate these dis-allowable characters in a character class [...]

    Dim pattern As String = "[\\@#$%\^]"

    Dim rx As New Regex(pattern)


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, April 23, 2010 3:36 PM

All replies

  • If (Regex.IsMatch(accountName, "[@#$%\^]")) Then

    End If

     This is called a "Character class"


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, April 23, 2010 3:50 AM
  •  

    dim disallowedCharacter as string = "\@#$%^", I missed \ on the list, is this all I have to do? Add \ on the list?

    Friday, April 23, 2010 3:25 PM
  •  

    dim disallowedCharacter as string = "\@#$%^", I missed \ on the list, is this all I have to do? Add \ on the list?


    The \ means what to follow is "literal". In this case the literal ^

    But you must encapsulate these dis-allowable characters in a character class [...]

    Dim pattern As String = "[\\@#$%\^]"

    Dim rx As New Regex(pattern)


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, April 23, 2010 3:36 PM