Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
谁能帮我写一个正则表达式?让一个字符串将“/n /n /n”合并到“/n/n”
例如:
"abc \n\n \n \n \n \n \n \n ddfdfd" === "abc \n\n ddfdfd"
所以只删除“\n”之间的空格。
您需要使用替换方法,将匹配的表达式替换为字符串。
听起来您想匹配\n[\s]*\n,并将其替换为 simple \n\n。
\n[\s]*\n
\n\n
这是一个快速的方法。
string s = "abc \n\n \n \n \n \n \n \n ddfdfd"; string s2 = Regex.Replace(s, @"\n\s*\n", "\n\n");