Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Created May 27, 2014 02:44
Show Gist options
  • Save dck-jp/af6634cbc92fd88978d4 to your computer and use it in GitHub Desktop.
Save dck-jp/af6634cbc92fd88978d4 to your computer and use it in GitHub Desktop.
/// <summary>
/// [Rules]
/// - all of lines (except first&last) require same indent
/// * indent = mix of tab and space is ok
/// - first line requires empty (only line break)
/// * linebreak = CR+LF or LF
/// - last line requires "no line break"
///
/// [SpecialThanks]
/// this code is same as sayurin's code (http://git.io/iE1MhA)
///
/// [Usage]
/// var x = @"
/// test
/// test".AsHereDocSimple();
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string AsHereDocSimple(this string input)
{
return Regex.Replace(input,
@"^(?=(?<sep>\r?\n)(?<indent>[ \t]*))(\k<sep>\k<indent>(?<text>[^\r\n]*))*$",
m => string.Join(
m.Groups["sep"].Value,
m.Groups["text"].Captures.Cast<Capture>().Select(c => c.Value)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment