Skip to content

Instantly share code, notes, and snippets.

@cakesmith
Forked from anonymous/balancedPatternMatcher.cs
Created November 19, 2015 20:14
Show Gist options
  • Save cakesmith/3c5d7f77a17a37a489d5 to your computer and use it in GitHub Desktop.
Save cakesmith/3c5d7f77a17a37a489d5 to your computer and use it in GitHub Desktop.
Balanced pattern matcher regular expression generator
// Match everything inside balancing groups ['<' and '>'] (Variables) and ['{' and '}'] (Macros)
// ex: <This is A Variable> and {This is a macro}
// http://goo.gl/Rjo6Ih
public static string PatternBuilder(char open, char close)
{
return $"(((?<open>{open})[^{open}]*)+([^{open}]*(?<close-open>{close}))+).*?(?(open)(?!))";
}
public static string caretPattern => PatternBuilder('<', '>');
public static string bracePattern => PatternBuilder('{', '}');
public static string parenthesisPattern => PatternBuilder('(', ')');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment