I am using while(matcher.find()) to loop through and retrieve things from a file. I was wondering how would I get a line number from within this loop, if I knew the index of what I have found is at matcher.start().
I am confused, could someone please explain?
String expr = "<[^<?!>]+>";
String[] response = new String[5];
Pattern p = Pattern.compile(expr);
Matcher m = p.matcher(xmlDocument);
while (m.find()) {
// System.out.println(m.group() + " located at " + m.start());
// txtMatches.append(m.group() + " located at " + m.start() + "\n");
if (itemStack.getCount() == 0 && m.group().contains("</")) {
response[0] = "Orphan closing tag" ;
response[1] = stripUnwantedChars(m.group(), true);
response[2] = String.valueOf(m.start()); //right here is where i want to return line number
return response;
}
//rest of code
itemStack is a stack of pushed matches and then I am comparing them to see if there is no more items in the stack but there is a match with a closing tag.
You need to separately create an array of the indexes where each line starts, and then you can use this array together with the index returned by
start()to figure out which line your match is on. A binary search of that line index array would do nicely. You could actually create this list of line indexes also by using a regex that matches a line end (matching just ‘\n’ would be fine) and then starting each line at the next character.