Skip to content

Instantly share code, notes, and snippets.

@theorigin
Created April 9, 2013 09:16
Show Gist options
  • Save theorigin/5344279 to your computer and use it in GitHub Desktop.
Save theorigin/5344279 to your computer and use it in GitHub Desktop.
RegEx to wrap [ ] around text. For example when you need to turn a single JSON object into an array.
/// A description of the regular expression:
///
/// Beginning of line or string
/// [1]: A numbered capture group. [[^[].*[^]]]
/// [^[].*[^]]
/// Any character that is NOT in this class: [[]
/// Any character, any number of repetitions
/// Any character that is NOT in this class: []]
/// End of line or string
var regex = new Regex("^([^[].*[^]])$", RegexOptions.CultureInvariant | RegexOptions.Compiled);
var result = regex.Replace(replacements, "[$1]");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment