regular expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon   Paal

    #1

    regular expressions

    need to verify a string against reg exp. to allow only characters and apostrophe and hyphen

    using :
    ========
    Function RegExpValidate( sInput,sPattern ) as boolean
    Dim RegexObj as Regex = New Regex("regulare xpression")
    return Regex.IsMatch(s Input,sPattern)
    End Function

    calling with:
    ==============
    RegExpValidate( MyString,"[a-zA-Z'-]")

    but pattern isn't working. any help ???


  • Herfried K. Wagner [MVP]

    #2
    Re: regular expressions

    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> schrieb:[color=blue]
    > need to verify a string against reg exp. to allow only characters and
    > apostrophe and hyphen
    >
    > using :
    > ========
    > Function RegExpValidate( sInput,sPattern ) as boolean
    > Dim RegexObj as Regex = New Regex("regulare xpression")
    > return Regex.IsMatch(s Input,sPattern)
    > End Function
    >
    > calling with:
    > ==============
    > RegExpValidate( MyString,"[a-zA-Z'-]")[/color]

    \\\
    Dim IsMatch As Boolean = _
    Regex.IsMatch(< Input>, "^[a-zA-Z'-]*$")
    ///

    You do not need to instantiate 'Regex' because 'IsMatch' is a shared method
    of the 'Regex' class.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Göran Andersson

      #3
      Re: regular expressions

      The hyphen is a special character in a set, you have to escape it:

      [a-zA-Z'\-]

      Jon Paal wrote:[color=blue]
      > need to verify a string against reg exp. to allow only characters and apostrophe and hyphen
      >
      > using :
      > ========
      > Function RegExpValidate( sInput,sPattern ) as boolean
      > Dim RegexObj as Regex = New Regex("regulare xpression")
      > return Regex.IsMatch(s Input,sPattern)
      > End Function
      >
      > calling with:
      > ==============
      > RegExpValidate( MyString,"[a-zA-Z'-]")
      >
      > but pattern isn't working. any help ???[/color]

      Comment

      • david

        #4
        Re: regular expressions

        On 2006-05-06, Göran Andersson <guffa@guffa.co m> wrote:[color=blue]
        > The hyphen is a special character in a set, you have to escape it:
        >
        > [a-zA-Z'\-][/color]

        You don't need to escape it if it appears at the end (or beginning) of the set.

        I suspect the OP is having problems with false positives rather than
        false negatives. The original Regex will match if any character in the
        string matches, rather than all. Presumably he wants either...

        ^[a-zA-Z'-]+$
        ^[a-zA-Z'-]*$

        depending on whether an empty string should match.

        [color=blue]
        >
        > Jon Paal wrote:[color=green]
        >> need to verify a string against reg exp. to allow only characters and apostrophe and hyphen
        >>
        >> using :
        >> ========
        >> Function RegExpValidate( sInput,sPattern ) as boolean
        >> Dim RegexObj as Regex = New Regex("regulare xpression")
        >> return Regex.IsMatch(s Input,sPattern)
        >> End Function
        >>
        >> calling with:
        >> ==============
        >> RegExpValidate( MyString,"[a-zA-Z'-]")
        >>
        >> but pattern isn't working. any help ???[/color][/color]

        Comment

        • Göran Andersson

          #5
          Re: regular expressions

          david wrote:[color=blue]
          > On 2006-05-06, Göran Andersson <guffa@guffa.co m> wrote:[color=green]
          >> The hyphen is a special character in a set, you have to escape it:
          >>
          >> [a-zA-Z'\-][/color]
          >
          > You don't need to escape it if it appears at the end (or beginning) of the set.[/color]

          Ah. Then that's not the problem.

          I would suggest that it's a good idea to escape it anyway. If you decide
          to add a character to the end of the set and forget to escape the
          hyphen, it would behave very unexpectedly, and it could take a lot of
          testing to realise the reason. :)
          [color=blue]
          > I suspect the OP is having problems with false positives rather than
          > false negatives. The original Regex will match if any character in the
          > string matches, rather than all. Presumably he wants either...
          >
          > ^[a-zA-Z'-]+$
          > ^[a-zA-Z'-]*$
          >
          > depending on whether an empty string should match.[/color]

          True, that is probably the reason why it "isn't working", as the OP put
          it... :)
          [color=blue][color=green]
          >> Jon Paal wrote:[color=darkred]
          >>> need to verify a string against reg exp. to allow only characters and apostrophe and hyphen
          >>>
          >>> using :
          >>> ========
          >>> Function RegExpValidate( sInput,sPattern ) as boolean
          >>> Dim RegexObj as Regex = New Regex("regulare xpression")
          >>> return Regex.IsMatch(s Input,sPattern)
          >>> End Function
          >>>
          >>> calling with:
          >>> ==============
          >>> RegExpValidate( MyString,"[a-zA-Z'-]")
          >>>
          >>> but pattern isn't working. any help ???[/color][/color][/color]

          Comment

          Working...