Seperating a line of text
I've got a text file, which I've broken down to read line by line. Each line is in the format of
word1 word 2 [word3] /word4/possible word5/...
So words1, 2, 3 and 4 will definately be there, there might be additional words, but these would be contained within forward slashes, where word three will be contained within square brackets, what sort of approach would I need to break these up?
Actually, you don't really need regex. you can use String.split(String str):
public String[] sort(String input)
{
String[] temp = input.split("/"); //All the words
String[] temp2 = temp[0].split(" "); //The first, second, and third words
String[] ret = new String[temp.length + temp2.length]; //The return
ret[0] = temp2[0];
ret[1] = temp2[1];
ret[2] = temp2[2].replaceAll("[", "").replaceAll("]", ""); /*Third word, without brackets*/
for(int i = 0; i < ret.length; i++)
ret[i + 3] = temp[ i ]; //assign the rest of the words
return(ret);
}
I'd think about debugging that piece of code...
Message was edited by:
atoxic
Message was edited by:
atoxic
I mean without making instances of regex. Sorry, I wasn't clear on that.
Alternatively, I thought up of an easier way
public String[] sort(String input){return(input.replace("[","").replace("/"," ").replace("] ","").split(" "));}
The spaces DO matter, so double-check on that.