ignore everything before and after the regex paterns strings

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 7735 Views - Last Post: 12 September 2016 - 01:12 AM Rate Topic: -----

#31 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: ignore everything before and after the regex paterns strings

Posted 11 September 2016 - 10:01 PM

There is a difference. You couldn't have been using groups[1] in both cases:

Output
Trying to match pattern: '(.*?)change:(.*?):change(.*?)'
>>> begin >>>
Match:'sometextchange:THIS IS A TEST:change':
Group[0]: 'sometextchange:THIS IS A TEST:change'
Group[1]: 'sometext'
Group[2]: 'THIS IS A TEST'
Group[3]: ''
<<< end <<<
Trying to match pattern: 'change:(.*?):change'
>>> begin >>>
Match:'change:THIS IS A TEST:change':
Group[0]: 'change:THIS IS A TEST:change'
Group[1]: 'THIS IS A TEST'
<<< end <<<



of this code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace SimpleCSConsole
{
    class Program
    {
        void WriteMatches(IEnumerable<Match> matches)
        {
            Console.WriteLine(">>> begin >>>");
            foreach (var match in matches)
            {
                Console.WriteLine($"Match:'{match}':");
                int index = 0;
                foreach (Group group in match.Groups)
                {
                    Console.WriteLine($"Group[{index}]: '{group.Value}'");
                    index++;
                }
            }
            Console.WriteLine("<<< end <<<");
        }

        IEnumerable<Match> GetMatches(string input, string pattern)
        {
            Console.WriteLine($"Trying to match pattern: '{pattern}'");
            return Regex.Matches(input, pattern, RegexOptions.Singleline).Cast<Match>();
        }

        void Run()
        {
            string myString1 = "change:";
            string myString2 = ":change";
            string myText = "sometextchange:THIS IS A TEST:changesometext";

            var pattern1 = "(.*?)" + myString1 + "(.*?)" + myString2 + "(.*?)";
            var pattern2 = $"" + myString1 + "(.*?)" + myString2;

            WriteMatches(GetMatches(myText, pattern1));
            WriteMatches(GetMatches(myText, pattern2));
        }

        static void Main()
        {
            new Program().Run();
            Console.ReadKey();
        }
    }
}



If you were using group[1] with your other search pattern, you would have gotten "sometext", not "THIS IS A TEST".
Was This Post Helpful? 1
  • +
  • -

#32 dr4   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 100
  • Joined: 19-December 14

Re: ignore everything before and after the regex paterns strings

Posted 12 September 2016 - 01:12 AM

I made the test with a small text and you are right about the output, in my program I'm using multiples matches (I have to find more than 1 match inside each text which in turn are part of a larger text), so after all the process the result is the same for me in both cases but individually they are sending different outputs.Thank you for your time, it really helped me to understand some concepts about regex.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3