implementing wildcard match function

A

Andrus

I need to implement vfp function which uses * and ? wildcards:

static bool Like( cExpression1, cExpression2)

cExpression1

Specifies the character expression that Like( ) compares with cExpression2.
cExpression1 can contain the wildcards such as * and ?. The question mark
(?) matches any single character in cExpression2 and the asterisk (*)
matches any number of characters. You can mix any number of wildcards in any
combination in cExpression1.

cExpression2
Specifies the character expression LIKE( ) compares with cExpression1.

returns true on wildcards match.

How to implement this ?

Andrus
 
T

Tom Dacon

The simplest and fastest way to do this would be to write a tiny assembly in
VB, and use the VB Like operator, which does exactly what you want. Put one
static (Shared in VB) function into it, that takes the two strings and
returns a Boolean.

E.g.:

Public Shared Function VBLike(Byval oneString as String, byval otherString
as String) as Boolean

Return oneString Like otherString

End Function

I hope this doesn't set off a flame war, and I'm sure that there's also a
way to do it with Regex, but it's a quick way to get what you want.

Actually, I'll confess that this response is a little tongue-in-cheek, but
what can I say :) Pretty quick someone will be along with the Regex
equivalent. If I don't see such a response after a while I'll work one up
for you in C#.

Tom Dacon
Dacon Software Consulting
 
A

Andrus

Tom,
Public Shared Function VBLike(Byval oneString as String, byval otherString
as String) as Boolean

Return oneString Like otherString

End Function

Thank you.
I have C# 2008 Express. It does not allow to program in VB.
I think there must be some method in some net dll whose call in generated by
VB compiler for like operator .

Do you know what it this method name so I can call it from C# code ?
Or is there vb -> c# converter which converts this code?

Andrus.
 
T

Tom Dacon

Andrus, here is the method name to use when you're calling from C#:

Microsoft.VisualBasic.CompilerServices.Operators.LikeString

However if what you're running is C# 2008 Express you may not actually have
the VB assemblies at all. Cross your fingers and go to the references dialog
and see what you find.

Otherwise, I'd pursue a solution with the Regex class in
System.Text.RegularExpressions. Try these sites to start:

http://www.regular-expressions.info/

http://regexlib.com/

One of those sites has a Regex tester that you can use to try out your
attempts.

Regex syntax is ugly and finicky, but very powerful. It's a "language" only
a programmer could love, and only a few do. I suspect that far less than one
percent of C# programmers are even competent with Regex expressions, let
alone fluent. However the rules you need to implement are simple, so perhaps
you might find your way to a suitable Regex expression without too much
difficulty.

Good luck,
Tom Dacon
 
T

Tom Dacon

Andrus, one of the messages in the discussion contains a link to a web site
with code that does it. The latter part of the message thread discusses
changes and extensions to the supplied code.

Good luck,
Tom Dacon
Dacon Software Consulting
 

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