regular expressions

J

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 ???
 
H

Herfried K. Wagner [MVP]

Jon Paal said:
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

[a-zA-Z'\-]
 
D

david

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.

Jon said:
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 ???
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

david said:
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... :)
Jon said:
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 ???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top