Sunteți pe pagina 1din 9

PL/SQL STRING PARSER

Author JP Vijaykumar Oracle DBA


Date Jan 4th 2012
C shell, Bourne shell, Korn shell.
Thanks Denise, your are unique.
This pl/sql string parser, parses sentenses, paragraphs and prints each word in
a separate line.
set serverout on size 1000000 timing on
declare
v_str varchar2(2000):=' the rows of gaping faces
staring at me make me mad.
words
will
refuse to come out of
my mouth.
fumbling and
stuttering
starts';
v_wrd varchar2(2000);
v_len number;
v_ins number;
v_chr char(1);
begin
--v_str:=upper(regexp_replace(regexp_replace(v_str,chr(10),' '),chr(9),' '));
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
--Will repace all non-printing characters and tabs with spaces
<<WHILE_LOOP>>
v_len:=length(v_str);
while (v_len > 0) loop
v_chr:=substr(v_str,1,1);
--dbms_output.put_line(v_chr||' '||length(v_chr));
if ( v_chr = ' ' ) then
v_str:=substr(v_str,2,length(v_str));
goto WHILE_LOOP;
else
v_ins:=instr(v_str,' ');
if
( v_ins = 0 ) then
v_len:=0;
v_wrd:=v_str;
elsif ( v_ins = 1 ) then
v_str:=substr(v_str,2,length(v_str));
goto WHILE_LOOP;
else
v_wrd:=substr(v_str,1,instr(v_str,' ') -1);
v_str:=substr(v_str,instr(v_str,' '), length(v_str));
v_len:=length(v_str);
end if;
end if;
dbms_output.put_line(v_wrd); --||' '||nvl(length(v_wrd),0));
end loop;
end;
/

THE
ROWS
OF
GAPING
FACES
STARING
AT
ME
MAKE
ME
MAD.
WORDS
REFUSE
TO
COME
OUT
OF
MY
MOUTH.
FUMBLING
AND
STUTTERING
STARTS
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
References:
http://waltsilva.wordpress.com/2007/10/12/using-regular-expression-functions-inplsql-to-check-for-complex-password-values/
http://stackoverflow.com/questions/2268860/trim-whitespaces-new-line-and-tab-spa
ce-in-a-string-in-oracle
https://forums.oracle.com/forums/thread.jspa?threadID=593376
http://laurentschneider.com/wordpress/category/sqlplus
http://download.oracle.com/docs/cd/B12037_01/server.101/b12170/ch13.htm
http://www.scribd.com/doc/15490898/Oracle-PLSQL-Language-Pocket-Reference
http://docstore.mik.ua/orelly/oracle/langpkt/ch01_04.htm
http://vapvarun.com/study/oracle/langpkt/ch01_04.htm
http://www.databasejournal.com/scripts/article.php/3678311
http://www.databasejournal.com/features/oracle/article.php/3684701
http://www.rocket99.com/techref/8712.html
--------------------------WRITTEN NOV 22ND 2013-----------------------------set serverout on size 1000000 timing on
declare
v_str varchar2(4000):='
Veeksha
Saketh
Ishha

';
v_wrd varchar2(4000):='';
begin
--v_str:=upper(regexp_replace(regexp_replace(v_str,chr(10),' '),chr(9),' '));
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
for i in 1..length(v_str) loop
if
if

( substr(v_str,i,1) = ' ' ) then


( length(v_wrd) > 0) then

dbms_output.put_line(v_wrd);
end if;
v_wrd:='';
elsif ( i = length(v_str) AND substr(v_str,i,1) = ' ' AND length(v_wrd) > 0)
then
dbms_output.put_line(v_wrd);
elsif ( i = length(v_str) AND substr(v_str,i,1) <> ' ' AND length(v_wrd) > 0)
then
v_wrd:=v_wrd||substr(v_str,i,1);
dbms_output.put_line(v_wrd);
elsif ( substr(v_str,i,1) <> ' ') then
v_wrd:=v_wrd||substr(v_str,i,1);
end if;
end loop;
end;
/
VEEKSHA
SAKETH
ISHHA
Elapsed: 00:00:00.01
set serverout on size 1000000 timing on
declare
v_str varchar2(4000):='
veeksha
ishha
saketh

';
v_wrd varchar2(4000):='';
begin
--v_str:=upper(regexp_replace(regexp_replace(v_str,chr(10),' '),chr(9),' '));
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
for i in 1..length(v_str) loop /*
if
( substr(v_str,i,1) = ' ' AND length(v_wrd) <> 0) then
dbms_output.put_line(v_wrd);
v_wrd:='';
elsif ( substr(v_str,i,1) = ' ' AND length(v_wrd) = 0) then
null;
elsif ( substr(v_str,i,1) <> ' ') then
v_wrd:=v_wrd||substr(v_str,i,1);
end if; */

if
( substr(v_str,i,1) = ' ' AND v_wrd IS NOT NULL) then
dbms_output.put_line(v_wrd);
---------------------START EXECUTING COMMANDS USING V_WRD
---------------------STOP EXECUTING COMMANDS USING V_WRD
v_wrd:='';
elsif ( substr(v_str,i,1) = ' ' AND v_wrd IS
NULL) then
null;
elsif ( substr(v_str,i,1) <> ' ') then
v_wrd:=v_wrd||substr(v_str,i,1);
end if;
end loop;
end;
/
--------------------------WRITTEN SEP 14TH 2014-----------------------------http://waltsilva.wordpress.com/2007/10/12/using-regular-expression-functions-inplsql-to-check-for-complex-password-values/
--A SIMPLIFIED PL/SQL STRING PARSER
set serverout on size 1000000 timing on
declare
v_str varchar2(2000):=' the ,rows, of ,gaping ,faces
,staring ,at, me, make ,me ,mad.,
words ,
will
,refuse ,to ,come ,out ,of
,my ,mouth.,
fumbling ,and
,stuttering
,starts';
begin
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
while (length(v_str)>0) loop
if (instr(v_str,',')>0 ) then
dbms_output.put_line(trim(substr(v_str,1,instr(v_str,',') -1)));
v_str:=substr(v_str,instr(v_str,',')+1,length(v_str));
else
dbms_output.put_line(trim(v_str));
v_str:='';
end if;
end loop;
end;
/
THE
ROWS
OF
GAPING
FACES
STARING
AT
ME
MAKE
ME
MAD.
WORDS
WILL

REFUSE
TO
COME
OUT
OF
MY
MOUTH.
FUMBLING
AND
STUTTERING
STARTS
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.05
--------------------------WRITTEN SEP 14TH 2014-------------------------------YET ANOTHER VERSION OF PL/SQL STRING PARSER
set serverout on size 1000000 timing on
declare
v_dlm char(1):=',';
v_str varchar2(2000):=' the ,rows, of ,gaping ,faces
,staring ,at, me, make ,me ,mad.,
words ,
will
,refuse ,to ,come ,out ,of
,my ,mouth.,
fumbling ,and
,stuttering
,starts';
begin
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
while (length(v_str)>0) loop
if (instr(v_str,v_dlm)>0 ) then
dbms_output.put_line(trim(substr(v_str,1,instr(v_str,v_dlm) -1)));
v_str:=substr(v_str,instr(v_str,v_dlm)+1,length(v_str));
else
dbms_output.put_line(trim(v_str));
v_str:='';
end if;
end loop;
end;
/
THE
ROWS
OF
GAPING
FACES
STARING
AT
ME
MAKE
ME
MAD.
WORDS
WILL
REFUSE
TO

COME
OUT
OF
MY
MOUTH.
FUMBLING
AND
STUTTERING
STARTS
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.05
--------------------------WRITTEN SEP 14TH 2014-----------------------------set serverout on size 1000000 timing on
declare
v_dlm char(1):=' ';
v_str varchar2(2000):=' the rows of gaping faces
staring at me make me mad.
words
will
refuse to come out of
my mouth.
fumbling and
stuttering
starts';
begin
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
while (length(v_str)>0) loop
if (instr(v_str,v_dlm)>0 ) then
dbms_output.put_line(trim(substr(v_str,1,instr(v_str,v_dlm) -1))||' '||length(tr
im(substr(v_str,1,instr(v_str,v_dlm) -1))));
v_str:=substr(v_str,instr(v_str,v_dlm)+1,length(v_str));
else
dbms_output.put_line(trim(v_str)||' '||length(trim(v_str)));
v_str:='';
end if;
end loop;
end;
/
THE 3
ROWS 4
OF 2
GAPING 6
FACES 5
STARING 7
AT 2
ME 2
MAKE 4
ME 2
MAD. 4
WORDS 5
WILL 4
REFUSE 6
TO 2
COME 4
OUT 3
OF 2

MY 2
MOUTH. 6
FUMBLING 8
AND 3
STUTTERING 10
STARTS 6
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.05
--------------------------WRITTEN SEP 14TH 2014-------------------------------THIS IS A SIMPLIFIED STRING PARSER
--THE ONLY PROBLEM INVOLVED IS, YOU NEED TO ENCAPSULATE EVERY STRING INSIDE
--"SELECT '....' FROM DUAL" AND JOIN WITH OTHER QUERIES WITH UNION ALL
--WHICH IS TIME CONSUMING AND DIFFICULT, IF THE TEXT IS IN PARAGRAPHS
select name from (
select 'YOU ' name from dual
union all
select 'ARE ' from dual
union all
select 'MY ' from dual
union all
select 'LITTLE ' from dual
union all
select 'PRINCESS ' from dual);
NAME
--------YOU
ARE
MY
LITTLE
PRINCESS
Elapsed: 00:00:00.02
--------------------------WRITTEN SEP 14TH 2014-----------------------------set serverout on size 1000000 timing on
declare
begin
for c1 in ( select name from (
select ' HAPPY ' name from dual
union all
select 'BIRTHDAY ' name from dual
union all
select 'MY ' name from dual
union all
select 'LITTLE ' name from dual
union all
select 'GIRL ' name from dual)) loop
dbms_output.put_line(c1.name);
end loop;
end;
/
HAPPY
BIRTHDAY

MY
LITTLE
GIRL
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.03
References:
I still passionately remember the great passage, I studied a few decades back,
"The rows of gaping faces, staring at me, make me mad, words refuse to come out
of my mouth, fumbling and stuttering starts" from the essay "On Lecturing" by R
obert Lynd
--------------------------WRITTEN NOV 22ND 2014-----------------------------set serverout on size 1000000 timing on
declare
begin
for c1 in (
select value from ((
select 'x' v1,'y' v2 , 'z' v3 from dual)
unpivot( value for value_type in (v1,v2,v3)))
) loop
dbms_output.put_line(c1.value);
end loop;
end;
/

select value from ((


select 'x' v1,'y' v2, 'z' v3 from dual)
unpivot( value for value_type in (v1,v2,v3)));
http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html
--------------------------WRITTEN JAN 4th 2015------------------------------set serverout on size 1000000 timing on
declare
v_str varchar2(2000):=' the rows of gaping faces
staring at me make me mad.
words
will
refuse to come out of
my mouth.
fumbling and
stuttering
starts';
begin
v_str:=upper(regexp_replace(v_str,'([[:cntrl:]])|(^\t)',' '));
for c1 in (
select name from (
select
regexp_substr(v_str,'[^ ]+', 1, level) as name,
rownum as nth
from dual
connect by regexp_substr(v_str, '[^ ]+', 1, level) is not null)) loop
dbms_output.put_line(c1.name);
end loop;
end;

http://stackoverflow.com/questions/10109335/how-to-get-the-nth-string-in-any-gen
eric-word-or-sentence-with-a-space-delimiter

S-ar putea să vă placă și