using System; using System.Text.RegularExpressions; public class Test { public static void Main() { string s = "[1]Ali ahmadi,[2]Mohammad Razavi"; Regex regex = new Regex(@"\[(\d+)\]", RegexOptions.Compiled); foreach (Match match in regex.Matches(s)) { Console.WriteLine(match.Groups[1].Value); } } }