none
Regular Expression not working, Changes are not allowed while code is running RRS feed

  • Question

  • Well I have 2 problems.

    Capture[0] should just be the number.

    Here is the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string x = @"c:\whirlwind\\Customer Support_backup011.bak";
                Regex r1 = new Regex("[0-9]{3}.bak");
                Console.WriteLine(r1.IsMatch(x));
                string pattern1 = "([0-9]{3}).bak";
                MatchCollection m1 = Regex.Matches(x, pattern1);
                int count1 = m1.Count;
                Console.WriteLine("Count of matches:" + count1.ToString());
                if (count1 > 0) { 
                    Console.WriteLine(m1[0]);
                    Match m0 = m1[0];
                    Group g1 = m0.Groups[0];                
                    CaptureCollection c1 = g1.Captures;
                    Console.WriteLine("Captures: " + c1.Count.ToString());
                    if (c1.Count > 0) { 
                        Capture c0 = c1[0];
                        Console.WriteLine("Capture[0] = " + c0);
                    }
                    Console.WriteLine("Captures count: " + c1.Count.ToString());
    
                    
                }
                Console.ReadLine();
            }
        }
    }
    /*
    True
    Count of matches:1
    011.bak
    Capture[0] = 011.bak  (This should just be 011)
    Captures count: 1
    
    Changes are not allowed while code is running (another problem, and yes the box is checked to enable it)
    
    */


    • Edited by cupboy1 Wednesday, September 25, 2019 5:09 PM
    Wednesday, September 25, 2019 5:06 PM

All replies

  • Hi cupboy1,

    Thank you for posting here.

    >>Capture[0] should just be the number.

    You could try the following change to get the correct result.

    Change:

                  Group g1 = m0.Groups[0];

    To:

                  Group g1 = m0.Groups[1];

    >>Changes are not allowed while code is running

    It means that you can't change the code when the code in debugging. You could close the program and change the code.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, September 26, 2019 5:34 AM
    Moderator