dtt2012
2018-10-01 15:08
浏览 652
已采纳

Golang替换所有换行符

Usually, when I'm replacing newlines I jump to Regexp, like in this PHP

preg_replace('/\R/u', "
", $String);

Because I know that to be a very durable way to replace any kind of Unicode newline (be it , , , etc.)

I was trying to something like this in Go as well, but I get

error parsing regexp: invalid escape sequence: \R

On this line

msg = regexp.MustCompilePOSIX("\\R").ReplaceAllString(html.EscapeString(msg), "<br>
")

I tried using (?:(?> )|\v) from https://stackoverflow.com/a/4389171/728236, but it looks like Go's regex implementation doesn't support that either, panicking with invalid or unsupported Perl syntax: '(?>'

What's a good, safe way to replace newlines in Go, Regex or not?


I see this answer here Golang: Issues replacing newlines in a string from a text file saying to use ? , but I'm hesitant to believe that it would get all Unicode newlines, mainly because of this question that has answer listing many more newline codepoints than the 3 that ? covers,

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题