我試圖從Java中使用 與匹配正則表達式提取一行的兩句話我行是這樣的,瀏覽器火狐=正則表達式在修剪前兩個字符
我使用下面的代碼
currentLine = currentLine.trim();
System.out.println("Current Line: "+ currentLine);
Pattern p = Pattern.compile("(.*?)=(.*)");
Matcher m = p1.matcher(currentLine);
if(m.find(1) && m.find(2)){
System.out.println("Key: "+m.group(1)+" Value: "+m.group(2));
}
我得到的輸出是 重點:OWSER值:火狐
BR在我的情況下,修去。這對我來說似乎很奇怪,直到我知道它爲什麼會這樣,因爲這與PERL完美搭配。有人能幫我嗎?
什麼是'currentLine'的價值?在'System.out.println(「當前行:」+ currentLine);'中打印的那個。 – acdcjunior
@AdrianWragg(。*?)是無關的,它只是意味着一個非貪婪的匹配,這將使正則表達式引擎停止在第一個符號處,而不是吞噬一切,然後回溯。在這種情況下,這不是必須的,但可以稍微更高效。 – sundar