对于 C#,我现在尝试使用正则表达式将文本文件中的换行符 (\n) 替换为分号 (;),但前提是该行中有任何内容;
如果文本文件是:
This is the program
Hello World
那么我的返回就是
This is the program;
Hello World;
我正在尝试使用
my_str = Regex.Replace(val, "\n", ";");
但它也会影响没有内容的行。
最佳答案
尝试capturing 1+ characters followed by a newline ?
(.+)[\r\n]?
\1;\n
C# :
my_str = Regex.Replace(val, "(.+)[\r\n]?", "$1;\n");
关于C# - 用分号替换换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23740725/