2

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.

4
  • @TheLostMind that would only trim the start and end of the whole file.
    – Mena
    Apr 2, 2015 at 8:55
  • @Jakob, there might be a hidden space between the start of line and the tab. You should probably use a Pattern / Matcher idiom and debug whether anything is found. If not, you could add a wildcard in between start of line and tab in your Pattern, group it, and print the back-reference as a unicode code point.
    – Mena
    Apr 2, 2015 at 8:56
  • 1
    Edit crucial answer by anubhava too. Might actually answer your question fully if you were already seeing the first tab replaced.
    – Mena
    Apr 2, 2015 at 9:02
  • @Mena - Ah my bad.. He is reading everything at once.. So ya.. multiline pattern will work Apr 2, 2015 at 9:08

3 Answers 3

2

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

content = content.replaceAll("(?m)^\\s+","");`
3
  • 1
    Can't believe I forgot about that one. +1
    – Mena
    Apr 2, 2015 at 9:02
  • I'll test this shortly, have to work on something else atm. Thanks so far already! At least your ideas are new to me, so i'll upvote them. (ok i lied, i cant upvote yet)
    – jz05185
    Apr 7, 2015 at 5:48
  • 1
    Works perfectly well, thanks a lot! Same goes to @karthik manchala, which is the same answer.
    – jz05185
    Apr 7, 2015 at 11:25
0

Use ^\t with multiline modifier.

str.replace("(?m)^\t", "");

DEMO

0

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 that illustrates trim().

1
  • Ok seems legit, but I've not copied all code to the question. Even though .trim() would've been better for the thing I've directly asked about, I do some more stuff also in the lines below that require RegExps anyway. Thanks though, help appreciated, didn't know about trim yet either.
    – jz05185
    Apr 7, 2015 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.