Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# snippet needed to replicate VBA Like operator

Tags:

c#

regex

vba

I am converting VBA code that contains the LIKE operator, as in

    dim sToken as String
    if sToken Like "(*,*)" then ...

In all cases the patterns use only the * wildcard which matches any string (including the empty string). The VBA Like operator yields only a true/false result so it's up to the subsequent VBA code to parse further and pluck out the matching substrings whenever there's a match.

I'd be most appreciative if someone can provide a C# snippet to test for the same type of simple wildcard match. If the snippet also yields matching substrings - even better.

like image 664
tpascale Avatar asked Feb 28 '11 17:02

tpascale


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Several people have suggested regular expressions which should work well for this scenario. Another option is to use the VB Like operator directly from C# code. This can be done by invoking the Compiler helper LikeOperator.LikeString. This function is located in the VB runtime assembly, Microsoft.VisualBasic.dll, and is usable from C#.

using Microsoft.VisualBasic.CompilerServices;

...

if (LikeOperator.LikeString(sToken, "(*,*)")) { 
  ...
}

I don't believe this version has 100% parity with the VBA version of Like but it will be extremely close and will match for the common scenarios.

like image 83
JaredPar Avatar answered Sep 28 '22 07:09

JaredPar


Well, that particular pattern could be matched with

if (sToken.StartsWith("(") && sToken.EndsWith(")")
    && sToken.Contains(","))

but in general you may find it makes more sense to use regular expressions. For example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        Regex regex = new Regex(@"^\(.*,.*\)$");

        Console.WriteLine(regex.IsMatch("x(a,b)")); // False due to the x
        Console.WriteLine(regex.IsMatch("(a,b)x")); // False due to the x
        Console.WriteLine(regex.IsMatch("(ab)"));   // False due to the lack of ,
        Console.WriteLine(regex.IsMatch("(a,b"));   // False due to the lack of )
        Console.WriteLine(regex.IsMatch("(a,b)"));   // True!
        Console.WriteLine(regex.IsMatch("(aaa,bbb)"));   // True!
        Console.WriteLine(regex.IsMatch("(,)"));   // True!
    }
}

Things to note with the pattern here:

  • I've used a verbatim string literal (the @ at the start) to make it easier to perform escaping within the regex
  • ^ and $ force it to match the whole string
  • The brackets are escaped so they're not treated as grouping operators

The MSDN "Regular Expression Language Elements" page is a good reference for .NET regexes.

like image 22
Jon Skeet Avatar answered Sep 28 '22 05:09

Jon Skeet