Question: I'm parsing some file into my Java-program. Due to its non-csv/tsv nature, I wanted to format it beforehand, so I can read it in easily. For this I tried regular expressions, which work fine for me with one exception: Start of line.

My files still have a tab at each line's start, which I want to get rid of, so that the csv-parser can properly parse them:

`^` = only if first in a line, `\t` = tab

```
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("^\t","");
Files.write(path, content.getBytes(charset));

```

Strange thing is, if I put my file and the regExp above into <http://myregexp.com/>, it marks the tabs to be deleted just fine. I went into the debugger, but couldn't find the error either, even after thorough search. I showed the code to two experienced Java programmers, to reduce the probability of the problem being completely trivial.

I've searched some examples from this site and others, replacing with empty String `""` is also ok. Besides, it worked at other places in my files. Just not at the start of line, it seems.

Answer: 
To remove whitespace (space/tab) from **start of each line** use `MULTILINE` flag:

```
content = content.replaceAll("(?m)^\\s+","");`

```
You do not need regexes for this kind of thing, you can leverage `String.Trim()` method.

Read the lines in, and use the following code:

```
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
   String line;
   while ((line = br.readLine()) != null) {
      line = lines.trim();
      // Then, write to another file stream
   }
}

```

See [sample program](http://goo.gl/J1ZSf3) that illustrates `trim()`.