Posted By

blackf0rk on 05/25/10


Tagged

regex parse tags text regular Expression between


Versions (?)

Return string from between two matched strings


 / Published in: C#
 

This code will take a start and end string and then search for them within the subject string. It returns what is between the start and end strings. As an example, if I had the following text...

Some test text string, with a start and an end .

...I could call this function to get the text between the and the strings. This function will return: "and an end"

  1. public string ParseBetween(string Subject, string Start, string End)
  2. {
  3. return Regex.Match(Subject, Regex.Replace(Start, @"[][{}()*+?.\\^$|]", @"\$0") + @"\s*(((?!" + Regex.Replace(Start, @"[][{}()*+?.\\^$|]", @"\$0") + @"|" + Regex.Replace(End, @"[][{}()*+?.\\^$|]", @"\$0") + @").)+)\s*" + Regex.Replace(End, @"[][{}()*+?.\\^$|]", @"\$0"), RegexOptions.IgnoreCase).Value.Replace(Start, "").Replace(End, "");
  4. }

Report this snippet  

You need to login to post a comment.