22

I've been trying to have a File be read, which will then put the read material into a string. Then the string will get split by line into multiple strings:

absPath, _ := filepath.Abs("../Go/input.txt")
data, err := ioutil.ReadFile(absPath)
if err != nil {
    panic(err)
}
input := string(data)

The input.txt is read as:

a

strong little bird

with a very

big heart

went

to school one day and

forgot his food at

home

However,

re = regexp.MustCompile("\\n")
input = re.ReplaceAllString(input, " ")

turns the text into a mangled mess of:

homeot his food atand

I'm not sure how replacing newlines can mess up so badly to the point where the text inverts itself

Community
  • 1
  • 1
Orange Receptacle
  • 1,173
  • 3
  • 19
  • 40

2 Answers2

46

I guess that you are running the code using Windows. Observe that if you print out the length of the resulting string, it will show something over 100 characters. The reason is that Windows uses not only newlines (\n) but also carriage returns (\r) - so a newline in Windows is actually \r\n, not \n. To properly filter them out of your string, use:

re := regexp.MustCompile(`\r?\n`)
input = re.ReplaceAllString(input, " ")

The backticks will make sure that you don't need to quote the backslashes in the regular expression. I used the question mark for the carriage return to make sure that your code works on other platforms as well.

Tyrone Wilson
  • 4,328
  • 2
  • 31
  • 35
Jens Grabarske
  • 735
  • 6
  • 7
8

I do not think that you need to use regex for such an easy task. This can be achieved with just

absPath, _ := filepath.Abs("../Go/input.txt")
data, _ := ioutil.ReadFile(absPath)
input := string(data)
strings.Replace(input, "\n","",-1)

example of removing \n

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 3
    That wouldn't solve the issue though, since — as Jens mentioned — Windows newlines (`\r\n`) are causing the described effect when only the `\n` is replaced while leaving the `\r` in the string. – madsen Dec 06 '15 at 01:54
  • 9
    No reason to get aggressive. Your point about not necessarily needing regex is fine, but your answer doesn't help OP solve his actual problem. – madsen Dec 06 '15 at 02:05
  • 9
    If it wasn't for Jens' answer, OP wouldn't know that the `\r` was the problem (and OP's output is more than enough to know that that is in fact the case). Your answer just provides OP with a different way of getting where he already is. It's not about inability to modify a working solution, it's about the fact that your solution is ... well ... not a solution to OP's problem. – madsen Dec 06 '15 at 02:29
  • @madsen, you are basing your justification on the fact, that Jen's answer solved OP's problem. Up till now I see that 2 hours passed and OP has not told anything. Which might mean that Jen's answer has not fixed anything. – Salvador Dali Dec 06 '15 at 02:37
  • 2
    You got to be kidding. If you know what `\r` does, then you know that OP's output is caused by exactly that — not by whether or not he's using regexes. Your answer only works if you apply Jens' fix too, otherwise it won't make any difference at all. – madsen Dec 06 '15 at 12:06