Home All Groups Group Topic Archive Search About
Author
6 May 2006 7:18 PM
Jon Paal
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("regularexpression")
   return Regex.IsMatch(sInput,sPattern)
End Function

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

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

Author
6 May 2006 7:34 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> schrieb:
> 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("regularexpression")
>   return Regex.IsMatch(sInput,sPattern)
> End Function
>
> calling with:
> ==============
> RegExpValidate(MyString,"[a-zA-Z'-]")

\\\
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/>
Author
6 May 2006 7:53 PM
Göran_Andersson
The hyphen is a special character in a set, you have to escape it:

[a-zA-Z'\-]

Jon Paal wrote:
Show quoteHide quote
> 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("regularexpression")
>    return Regex.IsMatch(sInput,sPattern)
> End Function
>
> calling with:
> ==============
> RegExpValidate(MyString,"[a-zA-Z'-]")
>
> but pattern isn't working.  any help ???
Author
8 May 2006 8:41 AM
david
On 2006-05-06, Göran Andersson <gu***@guffa.com> wrote:
> The hyphen is a special character in a set, you have to escape it:
>
> [a-zA-Z'\-]

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.


Show quoteHide quote
>
> Jon Paal wrote:
>> 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("regularexpression")
>>    return Regex.IsMatch(sInput,sPattern)
>> End Function
>>
>> calling with:
>> ==============
>> RegExpValidate(MyString,"[a-zA-Z'-]")
>>
>> but pattern isn't working.  any help ???
Author
8 May 2006 11:53 AM
Göran_Andersson
david wrote:
> On 2006-05-06, Göran Andersson <gu***@guffa.com> wrote:
>> The hyphen is a special character in a set, you have to escape it:
>>
>> [a-zA-Z'\-]
>
> You don't need to escape it if it appears at the end (or beginning) of the set.

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. :)

> 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.

True, that is probably the reason why it "isn't working", as the OP put
it... :)

Show quoteHide quote
>> Jon Paal wrote:
>>> 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("regularexpression")
>>>    return Regex.IsMatch(sInput,sPattern)
>>> End Function
>>>
>>> calling with:
>>> ==============
>>> RegExpValidate(MyString,"[a-zA-Z'-]")
>>>
>>> but pattern isn't working.  any help ???