Brandon
Brandon

Reputation: 945

VB.Net String comparison and Wildcard values

Is it possible to do String comparison where one of the strings I am comparing against has wild cards and is generally just for formatting purposes. For example

Dim correctFormat as String = "##-##-###-##"
Dim stringToCheck = someClass.SomeFunctionThatReturnsAStringToCheck
If FormatOf(CorrectFormat) = FormatOF(StringToCheck) then
 Else
End if

I am aware of the made up FormatOf syntax, but I'm just using it to show what I am asking.

Upvotes: 0

Views: 4240

Answers (3)

sloth
sloth

Reputation: 101052

No need for regular expressions.

You can simply use the Like operator, which supports ?, * and # as wildcards and also character lists ([...], [!...])

So you simply change your code to:

If stringToCheck Like correctFormat Then

and it will work as expected.

Upvotes: 4

Christian Sauer
Christian Sauer

Reputation: 10889

As the previous post mentioned, you should use regular expressions for that purpose - they are way better for that task. Sadly, learning them can be confusing, especially finding bugs can be really annoying. I really like http://www.regular-expressions.info/ and http://regexpal.com/ for building and testing regexes before.

In VB.net use something like reg.ismatch

Upvotes: 1

Oded
Oded

Reputation: 498992

The way is to use regular expressions - that's what they are for.

This is the regular expression that matches the format you have posted:

^\d{2}-\d{2}-\d{3}-\d{2}$

Upvotes: 2

Related Questions