Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2015 20:06
Show Gist options
  • Save anonymous/5fd424f46b9d878170d4 to your computer and use it in GitHub Desktop.
Save anonymous/5fd424f46b9d878170d4 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