We help IT Professionals succeed at work.

Simulate 'Like' operator with RegEx

luca345
luca345 used Ask the Experts™
on
Hi

I need  to simulate the vb 'like' operator with a new  equivalent function.

I was try to use this function:

//
  Public Shared Function LikeOp(ByVal inputString As String, ByVal PatternString As String) As Boolean

        Return System.Text.RegularExpressions.Regex.IsMatch(inputString, PatternString)

    End Function
//

but return different result and is not the exact 'like' equivalent.

Someone can please help me to fix  this function ?

Best Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can find a C# function that permits to compare two words (typically names) and see if they sound alike. See: http://www.csharphelp.com/2006/11/c-soundex-utility/

This function is called soundex. Two strings w1 and w2 look alike when they have the same soundex value: soundex (w1) == soundex (w2).

Hoping this helps.
Top Expert 2011

Commented:
Why can't you use the LIKE operator directly?
Public Shared Function LikeOp(ByVal inputString As String, ByVal PatternString As String) As Boolean

	Return (inputString LIKE PatternString)

End Function

Open in new window

Author

Commented:
I need to operate in VB and C# in the some mode.

C# don't have the 'like' operator but in my vb project there are many calls.

I need to fix the LikeOp function with some result of original 'like'.

I don't known how is difficoult do this.

Can you help me please ?
Top Expert 2011

Commented:
You could create a VB.NET DLL that contains the LikeOp method (using the LIKE operator), and then you can reference it on your other projects.

Author

Commented:
Yes, But create a DLL for only one function is not good idea.

I think there are alternatives, I hope.
Top Expert 2011
Commented:
Another alternative is to directly invoke the VB LIKE operator.  Something like this:
' VB.NET
Public Shared Function LikeOp(ByVal inputString As String, ByVal PatternString As String) As Boolean

	Return Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString(inputString, PatternString, Microsoft.VisualBasic.CompareMethod.Text)

End Function

Open in new window

// C#
// Add reference to Microsoft.VisualBasic.dll
public static bool LikeOp(string inputString, string PatternString)
{
	return Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString(inputString, PatternString, Microsoft.VisualBasic.CompareMethod.Text);
}

Open in new window

But Microsoft advises against calling the LikeString directly from code.

Author

Commented:
This is a solution.

I have already thinked about it.

This is and VB6 library and in the future will be certanly removed.

I have recently migrated from VB6 to vB.net and for this reason I like to use only .NET library.

If there are not other solutions this can be good.

Author

Commented:
Someone can confirm if:

Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString

is supported by mono project please ?

Author

Commented:
I Have find myself an equivalent function that use regex.

However thank you.

Author

Commented:
Good