我使用java的Pattern.matches來匹配一個數據塊到正則表達式。數據塊可以是單行或多行。問題是,一旦我的數據超過15行(通常超過17-18行),我開始越來越stackoverflowerror。對於少於15行的數據,正則表達式工作正常。Pattern.matches()給StackOverflowError
在正則表達式的格式如下:
域名 - >空間 - > - >空間 - >數 - >空間 - > - >空間 - >數 - >換行
String regex = "^(([a-zA-Z0-9][a-zA-Z0-9\\-]*\\.)+([a-zA-Z]{2,})\\s*,\\s*\\d+\\s*,\\s*\\d+(\\r?\\n)?)+$";
數據塊i使用測試針對此正則表達式是本
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
abc.com, 123, 456
這是代碼:
String regex = "^(([a-zA-Z0-9][a-zA-Z0-9\\-]*\\.)+([a-zA-Z]{2,})\\s*,\\s*\\d+\\s*,\\s*\\d+(\\r?\\n)?)+$";
boolean valid = Pattern.matches(regex, data); //fails here
+1實際上在野外遇到這個同名的錯誤。 :) – Xion
提示1)你不必在這裏轉義'-':'[a-zA-Z0-9 \\ - ]'(即:'a-zA-Z-]')2)在那裏在使用'.matches()'時,無需使用'^'和'$'' – NullUserException
您是否需要組或非捕獲組?如果是這樣,替換'('用'(?:'。 – Thomas