Last active
May 27, 2019 12:16
Star
You must be signed in to star a gist
Go 言語で改行コードを変換する(正規表現以外の解) ref: https://qiita.com/spiegel-im-spiegel/items/f1cc014ecb233afaa8af
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"regexp" | |
) | |
var regxNewline = regexp.MustCompile(`\r\n|\r|\n`) //throw panic if fail | |
func convNewline(str, nlcode string) string { | |
return regxNewline.Copy().ReplaceAllString(str, nlcode) | |
} | |
func main() { | |
before := "あ\nい\rう\r\nえ" | |
fmt.Printf("%U\n", []rune(before)) | |
after := convNewline(before, "\n") | |
fmt.Printf("%U\n", []rune(after)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[U+3042 U+000A U+3044 U+000D U+3046 U+000D U+000A U+3048] | |
[U+3042 U+000A U+3044 U+000A U+3046 U+000A U+3048] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func convNewline(str, nlcode string) string { | |
return strings.NewReplacer( | |
"\r\n", nlcode, | |
"\r", nlcode, | |
"\n", nlcode, | |
).Replace(str) | |
} | |
func main() { | |
before := "あ\nい\rう\r\nえ" | |
fmt.Printf("%U\n", []rune(before)) | |
after := convNewline(before, "\n") | |
fmt.Printf("%U\n", []rune(after)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[U+3042 U+000A U+3044 U+000D U+3046 U+000D U+000A U+3048] | |
[U+3042 U+000A U+3044 U+000A U+3046 U+000A U+3048] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment