Get Strings Between - CSharp System

CSharp examples for System:String SubString

Description

Get Strings Between

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.IO;/*from  w w w .j  ava2 s .  co m*/
using System.Collections.Generic;
using System;

public class Main{
        // TODO: Test
        public static IEnumerable<string> GetStringsBetween(this string stringToParse, string beforeString, string afterString)
        {
            var regEx = new Regex(Regex.Escape(beforeString) + "(.*?)" + Regex.Escape(afterString), RegexOptions.Singleline | RegexOptions.Compiled);
            var matches = regEx.Matches(stringToParse);
            foreach (Match match in matches)
            {
                yield return match.Groups[1].Value;
            }
        }
}

Related Tutorials