我有一个分配来读取字符串并显示元组字符串和该字符串中的辅音字符串。例如,如果我有字符串:aaceb,元音串将是:aae,辅音字符串将是:cb。我自己尝试了一些东西,但是我有一些错误(错误从命令中的第3行开始)我无法弄明白为什么。非常感谢提前:D
set serveroutput on
accept p_sir prompt "Sir=";
declare
v varchar2(20):=&p_sir;
vocale varchar2(20);
consoane varchar2(20);
c varchar2(1);
begin
for i in 1..length(v) loop
c:=substr(v,i,1);
case
when c='a' then vocale:=vocale||c;
when c='e' then vocale:=vocale||c;
when c='i' then vocale:=vocale||c;
when c='o' then vocale:=vocale||c;
when c='u' then vocale:=vocale||c;
else consoane:=consoane||c;
end case;
--dbms_output.put_line(c);
end loop;
dbms_output.put_line('vocale: ' ||vocale||' consoane:'||consoane);
end;
/
答案 0 :(得分:1)
概念证明:
SQL> begin
2 dbms_output.put_line(translate('mathguy', 'zAEIOUaeiou', 'z'));
3 end;
4 /
mthgy
PL/SQL procedure successfully completed.
答案 1 :(得分:1)
使用正则表达式:
[^aeiou]
。[aeiou]
和非字母字符[^a-z]
,只留下辅音。像这样:
DECLARE
v_text VARCHAR2(200) := 'Your text string';
BEGIN
DBMS_OUTPUT.PUT_LINE(
'Vowels: ' || REGEX_REPLACE( v_text, '[^aeiou]', NULL, 1, 0, 'i' )
);
DBMS_OUTPUT.PUT_LINE(
'Consonants: ' || REGEX_REPLACE( v_text, '[aeiou]|[^a-z]', NULL, 1, 0, 'i' )
);
END;
/