如何在
Java中使用Oracle正则表达式来获得相同的结果,或者如何基于Oracle正则表达式构建Java正则表达式.
甲骨文:
declare
str1 varchar2(50);
begin
str1 := REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA', ',[^,]+,');
DBMS_OUTPUT.PUT_LINE(str1);
end;
结果:
, Redwood Shores,
Java的:
String str1 = null;
Pattern pattern = Pattern.compile(",[^,]+,");
Matcher matcher = pattern.matcher("500 Oracle Parkway, Redwood Shores, CA");
if (matcher.find())
{
str1 = matcher.group(1);
}
但是这个正则表达式在Java中不起作用.它失败并出现此异常:
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
at java.util.regex.Matcher.group(Matcher.java:538)
at mousemover.Playground.main(Playground.java:16)
也许有一些算法来映射正则表达式?