0

I have a legacy code written in VBScript that uses regular expressions I need to rewrite this code in c#. Unfortunately I can't map VBScript RegEx class with "Submatches" collection to respective C# code.

This is VBScript: RegExp=CreateObject("VBScript.RegExp"); RegExp.Pattern = ' some pattern RegExp.IgnoreCase=True; RegExp.Global=True; RegExp.Multiline=True;

Matches=RegExp.Execute(someText);
For Each Match In Matches Do
    If Match.SubMatches(4) <> Nothing Then
    ' some code goes next

Problem is: I can't figure out what is the equivalent C# Regex methods for "Submatches" of VBScript.

My C#:

        var Regex = new Regex(@"(\{\n?)|(""(""""|[^""]*)*"")|([^\},\{]+)|(,\n?)|(\}\n?)",  RegexOptions.IgnoreCase | RegexOptions.Multiline);
        var Matches = Regex.G(textBox1.Text);

        var Tree = new SimpleTree<string>();

        foreach (var Match in Matches)
        {

        }

the "Match" object in the "foreach" loop is an "object" and has no any "Submatch" members. How to implement Submatches in C#?

Evil Beaver
  • 379
  • 3
  • 12

1 Answers1

1

Try this:

Regex re = new Regex(@"(\{\n?)|(""(""""|[^""]*)*"")|([^\},\{]+)|(,\n?)|(\}\n?)",  RegexOptions.IgnoreCase | RegexOptions.Multiline);
MatchCollection matches = re.Matches(textBox1.Text);

var Tree = new SimpleTree<string>();

foreach (Match m in matches)
{
    if( m.Groups(4).Value  != null )
    {
          // do your stuff here
    }
}
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56