exec dbms_java.dropjava('JavaRegexSource');
exec dbms_java.dropjava('JavaRegex');
create or replace java source named JavaRegexSource as
import java.util.regex.*;
public class JavaRegex {
public static int regex_like(String SRCSTR,String PATTERN){
Pattern p = Pattern.compile(PATTERN);
Matcher m = p.matcher(SRCSTR);
return m.find() ? 1 : 0;
}
public static String regex_replace(String SRCSTR,String PATTERN,String REPLACESTR){
return SRCSTR.replaceAll(PATTERN,REPLACESTR);
}
public static String regex_substr(String SRCSTR,String PATTERN){
Pattern p = Pattern.compile(PATTERN);
Matcher m = p.matcher(SRCSTR);
return m.find() ? m.group() : "";
}
public static int regex_instr(String SRCSTR,String PATTERN){
Pattern p = Pattern.compile(PATTERN);
Matcher m = p.matcher(SRCSTR);
return m.find() ? m.start()+1 : 0;
}
public static int regex_count(String SRCSTR,String PATTERN){
Pattern p = Pattern.compile(PATTERN);
Matcher m = p.matcher(SRCSTR);
int willReturn = 0;
while (m.find()) willReturn++;
return willReturn;
}
// 位置と回数の指定版を多重定義
// 開始位置からの戻り読みで参照させるオプションが必要
// (?<=AB)C で 文字列ABCの開始位置指定で検索するケースに対応するため
public static String regex_replace(String SRCSTR,String PATTERN,String REPLACESTR,POSITION int,OCCURRENCE int){
}
public static String regex_substr(String SRCSTR,String PATTERN,POSITION int,OCCURRENCE int){
}
public static int regex_instr(String SRCSTR,String PATTERN,POSITION int,OCCURRENCE int){
}
public static int regex_count(String SRCSTR,String PATTERN,POSITION int){
}
}
/
create or replace function JavaRegex_Like(SRCSTR varchar2,PATTERN varchar2) return number
as language java NAME 'JavaRegex.regex_like(java.lang.String,java.lang.String) return int';
/
create or replace function JavaRegex_Replace(SRCSTR varchar2,PATTERN varchar2,REPLACESTR varchar2) return varchar2
as language java NAME 'JavaRegex.regex_replace(java.lang.String,java.lang.String,java.lang.String) return java.lang.String';
/
create or replace function JavaRegex_Substr(SRCSTR varchar2,PATTERN varchar2) return varchar2
as language java NAME 'JavaRegex.regex_substr(java.lang.String,java.lang.String) return java.lang.String';
/
create or replace function JavaRegex_Instr(SRCSTR varchar2,PATTERN varchar2) return number
as language java NAME 'JavaRegex.regex_instr(java.lang.String,java.lang.String) return int';
/
create or replace function JavaRegex_Count(SRCSTR varchar2,PATTERN varchar2) return number
as language java NAME 'JavaRegex.regex_count(java.lang.String,java.lang.String) return int';
/
conn test/test
col res2 for a15
col res3 for a15
col res4 for a15
select JavaRegex_Like('12345','[2-4].$') as res1,
JavaRegex_Replace('1A2B3C4A5B','[1-5](?=A)','X') as res2,
JavaRegex_Substr('111B222A333A','[1-5]+(?=A)') as res3
from dual;
RES1■RES2 ■RES3
----■----------■----
1■XA2B3CXA5B■222
select JavaRegex_Substr('111B222A333A','XXX') as res4,
JavaRegex_Instr ('111B222A333A','XXX') as res5,
JavaRegex_Instr ('111B222A333A','[0-9]+A') as res6,
JavaRegex_Count('AAAAA','AAA{3}') as res7
from dual;
RES4■RES5■RES6■RES7
----■----■----■----
null■ 0■ 5■ 1