下面提到的RegEx在非常大的字符串或超過2000行中執行得非常糟糕。基本上Java String由PL/SQL腳本組成。優化Java代碼中的多個RegEx
1-將每個出現的定界字符,例如||,!=或>符號替換爲字符前後的空格。這需要無限的時間,永不結束,所以不能記錄時間。
// Delimiting characters for SQLPlus
private static final String[] delimiters = { "\\|\\|", "=>", ":=", "!=", "<>", "<", ">", "\\(", "\\)", "!", ",", "\\+", "-", "=", "\\*", "\\|" };
for (int i = 0; i < delimiters.length; i++) {
script = script.replaceAll(delimiters[i], " " + delimiters[i] + " ");
}
2-以下模式查找所有正斜槓的發生/除了前面帶*的那些。這意味着不要在塊註釋語法中查找正斜槓。對於2000行的字符串,這需要大約103秒。
Pattern p = Pattern.compile("([^\\*])([\\/])([^\\*])");
Matcher m = p.matcher(script);
while (m.find()) {
script = script.replaceAll(m.group(2), " " + m.group(2) + " ");
}
3-從日期或日期格式
Pattern p = Pattern.compile("(?i)(\\w{1,2}) +/ +(\\w{1,2}) +/ +(\\w{2,4})");
// Create a matcher with an input string
Matcher m = p.matcher(script);
while (m.find()) {
part1 = script.substring(0, m.start());
part2 = script.substring(m.end());
script = part1 + m.group().replaceAll("[ \t]+", "") + part2;
m = p.matcher(script);
}
內刪除所有空格有沒有什麼辦法讓他們花費更少的時間來優化所有的三個正則表達式?
感謝
阿里
你應該把這個問題分成三個單獨的問題。這也將幫助您創建比「優化多個正則表達式」更有意義(因此也更有趣)的問題標題...... –