0

This is probably a duplicate but I m too dense to make this work. I m trying to learn capture groups etc but I cant make this one work, I get the right matches but only the last digit gets matched.

    class Program
   {

    static void Main(string[] args)
    {
        string testTxt = "sadsadas168.asdsad";
        string asdf = "wrongwrong";
        string abc = "abc.png";
        string bsdf = "100";
        string lolol = "155abx.text";
        string another = "sasd199.ong100";

        TryMatch(testTxt);
        TryMatch(asdf);
        TryMatch(abc);
        TryMatch(bsdf);
        TryMatch(lolol);
        TryMatch(another);

        Console.ReadLine();

    }

    private static void TryMatch(string txt)
    {
        var rgx = new Regex(@"\w*(?<t>\d+)\w*\..*");
        var m = rgx.Match(txt);

        if (m.Success)
        {
            int mainID = int.Parse(m.Groups["t"].Value);
            Console.WriteLine(m.Groups["t"].Value);
        }
        else
        {
            Console.WriteLine("Did not match " + txt);
        }
    }
}
T.Zak
  • 309
  • 2
  • 14

0 Answers0