Regex (C#): Replace \n with \r\n

前端 未结 7 1798
陌清茗
陌清茗 2020-12-13 18:11

How can I replace lone instances of \\n with \\r\\n (LF alone with CRLF) using a regular expression in C#?

Sorry if it\'s a stupid question, I\'m new to Regex.

相关标签:
7条回答
  • 2020-12-13 18:31

    If I know the line endings must be one of CRLF or LF, something that works for me is

    myStr.Replace("\r?\n", "\r\n");
    

    This essentially does the same neslekkiM's answer except it performs only one replace operation on the string rather than two. This is also compatible with Regex engines that don't support negative lookbehinds or backreferences.

    0 讨论(0)
提交回复
热议问题