Created
May 27, 2014 02:44
-
-
Save dck-jp/af6634cbc92fd88978d4 to your computer and use it in GitHub Desktop.
simplify the sayurin's code @ https://github.com/sayurin/HereDocumentExtension/blob/master/HereDocumentExtension/HereDocumentExtention.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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