CSharp examples for System:String Match
Lets you find everything between "{" and "}"
// file, You can obtain one at http://www.apache.org/licenses/ using System.Text.RegularExpressions; using System.Text; using System.Linq; using System.Collections.Generic; using System;//ww w .ja va 2 s . c om public class Main{ /// <summary> /// Lets you find everything between say "{" and "}", except that we allow /// ")" if they are proceeded by a "(" (exceptFor) /// </summary> /// <param name="findStart"></param> /// <param name="findEnd"></param> /// <param name="searchIn"></param> /// <param name="pos"></param> /// <returns></returns> public static string FindBetweenBalanced(this string searchIn, string findStart, string findEnd, ref int pos) { StringBuilder sb = new StringBuilder(); int balancer = 0; // Get to the start first up... while (pos <= searchIn.Length - findStart.Length) { if (searchIn.Substring(pos, findStart.Length) == findStart) { pos++; break; } pos++; } if (pos == searchIn.Length) { // Unable to find the start position return null; } while (pos <= searchIn.Length - findEnd.Length) { if (searchIn.Substring(pos, findEnd.Length) == findEnd) { if (balancer == 0) { return sb.ToString(); } else { balancer--; } } if (searchIn.Substring(pos, findStart.Length) == findStart) { balancer++; } sb.Append(searchIn[pos]); pos++; } // Didn't find it. return null; } }