2013-07-07 50 views
1

我在写一个动态代码编写器,并且正在编写一个写入XML文档的函数。一切都很好,除了格式化文档的函数不期望地添加到最后,我还需要它打破标点符号。将行附加到Replace末尾的正则表达式()

当前格式化功能:

public static String FormatForXmlDocumentation(String xmlLine, Int32 spacing, 
    Int32 length = 120) 
{ 
    if (!string.IsNullOrEmpty(xmlLine)) 
    { 
     var regex = new Regex(@".{0," + length.ToString() + "}", RegexOptions.Multiline); 
     return regex.Replace(xmlLine, "$0\r\n" + string.Empty.PadRight(spacing) + "/// "); 
    } 

    return null; 
} 

测试代码(表示结束 “!”):

Console.WriteLine(RenderForCode.FormatForXmlDocumentation(
@"Data info: Type: <see cref=""System.Nullable`1[System.Double]""/>; Nullable: false; Default: 101.23d; Low: 100d; High: 200d", 4, 40 
) + "!"); 

电流输出:

Data info: Type: <see cref="System.Nulla 
    /// ble`1[System.Double]"/>; Nullable: false 
    /// ; Default: 101.23d; Low: 100d; High: 200 
    /// d 
    /// 
    /// ! 

所需的输出:

Data info: Type: <see cref="System. 
    /// Nullable`1[System.Double]"/>; Nullable: 
    /// false; Default: 101.23d; Low: 100d; 
    /// High: 200d! 

请注意,调用函数将处理第一行的前缀。

回答

1

尝试使用该正则表达式:

@"\b.{0," + length.ToString() + @"}(?:(?!:)\b|$)" 

\b确保字不是在中间分开,$提供的最后一行。

+0

这也避免了将'Nullable:'分割为'Nullable \ r \ n:'这样的东西,但是如果还有更多类似的东西,请将它们添加到字符类中的负向预览中。 – Jerry

+0

谢谢,但这实际上并没有分割输入,并且仍然在最后添加2行。 – IamIC

+0

var regex = new Regex(@“\ b。{0,”+ length.ToString()+“}(?:(?!:)\ b | $)”); return regex.Replace(xmlLine,“$ 0 \ r \ n”+ string.Empty.PadRight(spacing)+“///”); – IamIC