java - How to split a string without losing any word? -
i using eclipse java , want split input line without losing characters.
for example, input line is:
ipod6 1 usd6iphone6 16g,64g,128g usd9,usd99,usd999macair 2013-2014 usd123macpro 2013-2014,2014-2015 usd899,usd999 and desired output is:
ipod6 1 usd6 iphone6 16g,64g,128g usd9,usd99,usd999 macair 2013-2014 usd123 macpro 2013-2014,2014-2015 usd899,usd999 i using split("(?<=\\busd\\d{1,99}+)") doesn't work.
you need add non-word boundary \b inside positive look-behind. \b matches between 2 non-word characters or between 2 word characters. won't split on boundary exists between usd9 , comma in usd9, substring because there word boundary exits between usd9 , comma since 9 word character , , non-word character. splits on boundary exists between usd6 , iphone6 because there non-word boundary \b exists between substrings since 6 word character , i word character.
string s = "ipod6 1 usd6iphone6 16g,64g,128g usd9,usd99,usd999macair 2013-2014 usd123macpro 2013-2014,2014-2015 usd899,usd999"; string[] parts = s.split("(?<=\\busd\\d{1,99}+\\b)"); for(string i: parts) { system.out.println(i); } output:
ipod6 1 usd6 iphone6 16g,64g,128g usd9,usd99,usd999 macair 2013-2014 usd123 macpro 2013-2014,2014-2015 usd899,usd999
Comments
Post a Comment