我使用while(matcher.find())
循環並從文件中檢索事物。我想知道如果我知道我發現的索引是matcher.start()
,我將如何從這個循環中得到一個行號。使用matcher.start()獲取matcher.find()中的行號
我很困惑,請問有人能解釋一下嗎?
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
是推火柴堆,然後我比較它們,看看是否有在棧中沒有更多的項目,但有一個結束標記匹配。
您是爲每一行構建一個單獨的匹配器,還是爲整個文件內容構建一個單獨的匹配器?一些代碼會很好。 –
不必爲每行分開匹配器。一個用於整個文件。並通過找到所有文本循環使用這個正則表達式=「<[^!>] +>」讓我看看我是否可以簡化一些代碼張貼 – Pengume