Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

Understanding regex in Java: split(" ") vs split("\t") - when do they both work, and when should they be used

I have recently figured out that I haven't been using regex properly in my code. Given the example of a tab delimited string str, I have been using str.split(""). Now I realize that this is wrong and to match the tabs properly I should use str.split("\t").

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 split("")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...

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: item1,item2,item3 and I would like to parse them as separate elements, for the sake of simplicity. In that case the appropriate regex expression should be: line.split("[\t,]"), or am I mistaken here as well??

Thanks in advance,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When using "", the escape sequence is replaced by Java with the character U+0009. When using "\t", the escape sequence \ in \t is replaced by Java with , resulting in that is then interpreted by the regular expression parser as the character U+0009.

So both notations will be interpreted correctly. It’s just the question when it is replaced with the corresponding character.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...