开发者

Splitting string by parenthesis

开发者 https://www.devze.com 2023-03-24 06:58 出处:网络
I have a string like abcdef(1)ghijkllskjdflkjsdfsdf(2)aslkdjfjgls(3)jgjgjkdkgkdll I want to spli开发者_开发百科t it into n number of lines depending on occurences of (n) in the string.

I have a string like abcdef(1)ghijkllskjdflkjsdfsdf(2)aslkdjfjgls(3)jgjgjkdkgkdll

I want to spli开发者_开发百科t it into n number of lines depending on occurences of (n) in the string.

For example in above string, following is acheived :

lines [0] = abcdef
lines [1] = ghijkllskjdflkjsdfsdf
lines [2] = aslkdjfjgls
lines [3] = jgjgjkdkgkdll.

What i am trying is :

StringBuilder sb = new StringBuilder();

var pattern = @"((.*))"; 

string[] lines = Regex.Split(text,pattern);

foreach (string line in lines)
{
    sb.AppendLine(line);
}

string FinalText = sb.ToString();

Can anyone help with C# regular expressions or string split function ?

Thank you.


string pattern = @"\(\d+\)";
string[] lines = Regex.Split(text,pattern);
string finalText = String.Join(Environment.NewLine, lines);


The follow regex will match your numbered brackets:

\(\d+\)

Your usage of Regex.Split is correct, so I do not know why you need help with that!?

0

精彩评论

暂无评论...
验证码 换一张
取 消