Created
May 27, 2014 02:46
-
-
Save dck-jp/9f73846aa14411b93ebc to your computer and use it in GitHub Desktop.
simify HereDocumentExtension.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) require same indent | |
/// * indent = mix of tab and space is ok | |
/// - first line requires empty (only line break) | |
/// * linebreak = CR+LF or LF | |
/// | |
/// [SpecialThanks] | |
/// this code is inspired by sayurin's code (http://git.io/iE1MhA) | |
/// | |
/// [Usage] | |
/// var x = @" | |
/// test | |
/// test".AsHereDocSimpleType2(); | |
/// var x = @" | |
/// test | |
/// test | |
/// ".AsHereDocSimpleType2(); | |
/// </summary> | |
/// <param name="input"></param> | |
/// <returns></returns> | |
public static string AsHereDocSimpleType2(this string input) | |
{ | |
return Regex.Replace(input, | |
@"^(?<sep>\r?\n)(?<indent>[ \t]*)((?<text>[^\r\n]*)\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