Hello,
How can I have a text file (or XML file) represented as a whole string, and search for (or match) a particular string in it?
I have created a BufferedReader object:
BufferedReader input = new BufferedReader(new FileReader(aFile));
and then I have tried to use the Scanner class with its option to specify different delimiters, like this:
//Scanner scantext = new Scanner(input);
//Scanner scantext = new Scanner(input).useDelimiter("");
Scanner scantext = new Scanner(input).useDelimiter("\n");
while (scantext.hasNext()) { ... }
Using the Scanner class like this I can either read the text line by line, or word by word, but it doesn't help me, because sometimes in the text, which I want to process, I have
</review><review>
and I would like to say: if you find "<review>
" anywhere in the text, do something with the following next lines (or piece of text) until you find "</review>
". The problem is that <review>
and </review>
are on different places in the text, and sometimes glued to other text (therefore the empty space as delimiter doesn't help me).
I have thought that I might use the regular expression API in Java (the Pattern and Matcher classes), but they seem to match a particular string or line, and I want to have the text as one continuous string (at least this was my impressions from what I have read about them). Could you tell me what structures/methods/classes I should use in this case? Thank you.