c# - Regex - Getting data from inside round brackets separated by commas -


this question has answer here:

i still learning regex , new it.

i have following data

add(2,3); 

i integer values within parenthesis '()' while getting individual values of following integers separated ',' stored variable in array list

in expected outcome should be

result[0] = 2; result[1] = 3; 

another sample data

add(2,3,1); 

and result following

result[0] = 2; result[1] = 3; result[2] = 1; 

i have tried using following expression ' @"\d+"' when i'm parsing data reads digits in string. expression have tried '((\d\,\d))' reads first example not second.

whole code snippet

   string s = "add(2,3,1);";    matchcollection matches = regex.matches(s, @"\d+");     string[] result = matches.cast<match>().take(10).select(match => match.value).toarray(); 

kindly please advise. thank you.

i use named groups, , iterate through different captures of group, following. added dummy numbers in test string make sure not captured:

string s = "12 3 4 142  add( 2 , 3,1 ); 21, 13 123 123,"; var matches = regex.matches(s, @"\(\s*(?<num>\d+)\s*(\,\s*(?<num>\d+)\s*)*\)");  foreach (match match in matches)     foreach (capture cpt in match.groups["num"].captures)         console.writeline(cpt.value); 

to store captures in array, can use following linq statement:

var result = matches.cast<match>()                     .selectmany(m => m.groups["num"].captures.cast<capture>())                     .select(c => c.value).toarray(); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -