The question is published on by Tutorial Guruji team.
I’m trying to parse a text document containing region names and a list of coordinates that comprise the region. The text is not structured in an easily parse-able way, as it is written freeform like this:
GUZ06—Caboolture River
The area bounded by a line starting at the intersection of the marine park boundary on the mainland and the parallel of latitude 27°08.981′ south (at or about the point of 27°08.981′ south, 153°01.822′ east) then running progressively— (a) generally north-westerly and south-easterly (via the Caboolture River) along the marine park boundary on the mainland to its intersection with the meridian of longitude 153°02.197′ east (at or about the point of 27°08.762′ south, 153°02.197′ east); and
…
GUZ07-[…]
What I’d like to be able to do is match against the name of a region, and then find where the next region is, and extract the block of text between the two match points, and run my coordinate extraction logic on that block of text, using something like:
while (matcher.find()) { int textStart = matcher.end() + 1; //remember the end of the current title matcher.find(); //find the start of the next title String regionData = myBigString.substring(textStart, matcher.start()); //extract the text for this region //[process the region data] matcher.forgetLastFind(); //need to go back so that the next iteration starts from the correct place }
Of course, forgetLastFind()
isn’t a real thing. Is there any way to approximate this behavior using the Matcher
API? Ideally I would like something like Stack.peek()
which returns the next element without actually modifying the internal state of the data structure.
Answer
You can use Matcher.find(int)
to reset the matcher and start search from the remembered point.