Reputation: 570
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.
Upvotes: 2
Views: 3889
Reputation: 692
You can use a reverse method to get line number by creating a region from 0 to character number returned from start().
For example,
class MatchTest {
public static void main(String...args) {
try {
FileInputStream fis = new FileInputStream("source.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String data = new String(buffer);
fis.close();
Pattern pattern = Pattern.compile(args[0]);
Matcher matcher = pattern.matcher(data);
while(matcher.find()) {
out.println(matcher.group());
out.println(getLine(data, matcher.start()));
}
}
catch(Exception e) {
e.printStackTrace();
}
}
static int getLine(String data, int start) {
int line = 1;
Pattern pattern = Pattern.compile("\n");
Matcher matcher = pattern.matcher(data);
matcher.region(0, start);
while(matcher.find()) {
line++;
}
return(line);
}
}
Here, the getLine method will return line number.
Upvotes: 4
Reputation: 81684
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.
Upvotes: 3