简体   繁体   English

C#使用正则表达式组拆分没有子字符串的字符串

[英]C# Split String without sub string using regex group

The String Like this:像这样的字符串:

Select(GetName(null),GetID(22),1,GetID(),GetData("T",100),true);

I want to split the string (function).我想拆分字符串(函数)。

Return like this:像这样返回:

String MainFunName = "Select()"
String MainFunName_Parameters="GetName(null),GetID(22),1,GetID(),GetData("T",100),true"
int MainFunName_Parameters1_Count = 6;

GetName(null)
GetID(22)
1
GetID()
GetData("T",100)
true

I was try我是尝试

_str.Substring(_str.IndexOf("(") + _str.Length).Split(',').Count();

It will get 7 not 6, the GetData("T",100) was splited to two part它将得到 7 而不是 6,GetData("T",100) 被分成两部分

GetData("T"
100)

I would like to know how to use the regex group to split this string?我想知道如何使用正则表达式组来拆分这个字符串? thanks谢谢

You can use it like this:你可以这样使用它:

String str = "Select(GetName(null),GetID(22),1,GetID(),GetData(\"T\",100),true)";
Match result = Regex.Match(str, @"^(\w+)\(([\w""]+(\(.*?\))?[\s,]*?)*\)$");

string outerMethodName = result.Groups[1].Value;
List<string> arguments = result.Groups[2].Captures.Cast<Capture>().Select(i => i.Value.TrimEnd(',')).ToList();

Console.WriteLine(outerMethodName);

int argumentLength = arguments.Count;
foreach (string argument in arguments)
{
    Console.WriteLine(argument);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM