title: Understanding regex in Java: split("\t") vs split("\\t") - when do they both work, and when should they be used
body: <p>I have recently figured out that I haven't been using regex properly in my code. Given the example of a tab delimited string <code>str</code>, I have been using <code>str.split("\t")</code>. Now I realize that this is wrong and to match the tabs properly I should use <code>str.split("\\t")</code>.</p>
  
  <p>However I happen to stumble upon this fact by pure chance, as I was looking for regex patterns for something else. You see, the faulty code <code>split("\t")</code>has been working quite fine in my case, and now I am confused as to why it does work if it's the wrong way to declare a regex for matching the tab character. Hence the question, for the sake of actually understanding how regex is handled in Java, instead of just copying the code into Eclipse and not really caring why it works...</p>
  
  <p>In a similar fashion I have come upon a piece of text which is not only tab-delimited but also comma delimited. More clearly put, the tab-delimited lists I am parsing sometimes include "compound" items which look like: <code>item1,item2,item3</code> and I would like to parse them as separate elements, for the sake of simplicity. In that case the appropriate regex expression should be: <code>line.split("[\\t,]")</code>, or am I mistaken here as well?? </p>
  
  <p>Thanks in advance,</p>