例子1
declare
names varchar2(60) := 'Anna,Matt,Joe,Nathan,Andrew,Aaron,Jeff';
names_adjusted varchar2(61);
comma_delimited boolean;
begin
comma_delimited := regexp_like(names,'^([a-z A-Z]*,)+([a-z A-Z]*)$');
dbms_output.put_line(
case comma_delimited
when true then
'We have a delimited list!'
else
'The pattern does not match.'
end
);
end;
例子2
declare
contact_info varchar2(200) := '
address:
1060 W. Addison St.
Chicago, IL 60613
home 773-555-5253
';
phone_pattern varchar2(90) :=
'\(?\d{3}\)?[[:space:]\.\-]?\d{3}[[:space:]\.\-]?\d{4}';
begin
dbms_output.put_line('The phone number is: '||
regexp_substr(contact_info,phone_pattern,1,1));
end;
例子3
declare
contact_info varchar2(200) := '
address:
1060 W. Addsion St.
Chicago, IL 60613
home 777-555-5253
work (312) 555-1234
cell 224.555.2233
';
phone_pattern varchar2(90) :=
' \(?(\d{3})\)?[[:space:]\.\-]?(\d{3})[[:space:]\.\-]?(\d{4})';
contains_phone_nbr boolean;
phone_number varchar2(15);
phone_counter number;
area_code varchar2(3);
begin
contains_phone_nbr := regexp_like(contact_info,phone_pattern);
if contains_phone_nbr then
phone_counter := 1;
dbms_output.put_line('The phone numbers are:');
loop
phone_number := regexp_substr(contact_info,phone_pattern,1,phone_counter);
exit when phone_number is null;
dbms_output.put_line(phone_number);
phone_counter := phone_counter + 1;
end loop;
phone_counter := 1;
dbms_output.put_line('The area codes are:');
loop
--最后的那个1表示返回第一个子表达式。子表达式是指模式里用圆括号括住的部分
--不知道为什么,当我使用3表示返回第三个表达式的时候,提示ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小
area_code := regexp_substr(contact_info, phone_pattern,1,phone_counter,'i',1);
exit when area_code is null;
dbms_output.put_line(area_code);
phone_counter := phone_counter + 1;
end loop;
end if;
end;
例子4
declare
names varchar2(200) := 'http://zhengwu.beijing.gov.cn/yjgl/yjya/t832821.htm';
names_adjusted varchar2(61);
comma_delimited boolean;
begin
comma_delimited := regexp_like(names,'^(http://zhengwu.beijing.gov.cn/yjgl/yjya/)t([0-9]+)(\.htm)$');
dbms_output.put_line(
case comma_delimited
when true then
'We have a delimited list!'
else
'The pattern does not match.'
end
);
end;