CSharp · Console
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var text = @"(example (to (parsing nested paren) but) (first lvl only))";
var pattern = @"\(([\w\s]+) (\([\w\s]+ \([\w\s]+\) [\w\s]+\)) (\([\w\s]+\))\)*";
string[] results = new string[]{};
try
{
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(text);
string group_1 = m.Groups[1].Value; //example
string group_2 = m.Groups[2].Value; //(to (parsing nested paren) but)
string group_3 = m.Groups[3].Value; //(first lvl only)
results = new string[]{group_1,group_2,group_3};
}
catch(Exception ex){
results = new string[]{"error"};
}
for (int i=0;i<results.Length;i++){
Console.WriteLine(results[i]) ;
}
}
}