Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with regexp and text split

BeantownAug 12 2022

I have this text that I need to split at 80 characters, the split count should always start at the beginning of a new line in the paragraph. I am not much of an expert on regular expression, I have come up with a code snippet below that takes the entire string and splits at 80 char length. But it is not splitting it where I wanted it to. In the sample output below the code, if you see the length of third line its not 80, because it counted the salutation line and new empty line char also in the 80 char count. Line 9 simple has 'I'. Likewise the title on the last line got split into two lines.
How do I make the regexp to only start splitting at the beginning of the line. I would greatly appreciate any help from the regexp experts.

declare
   v_txt        clob;
   v_out        clob;
   v_tmp        varchar2(500);
   v_len        number;
begin
     v_txt := 'Dear Event Planning Experts Inc,

Thank you so much for your assistance in planning our annual meeting. Your expertise in handling the meeting arrangements, booking the conference facilities and hotel, coordinating travel, scheduling events, and organizing the meeting is greatly appreciated.

I appreciate your help and advice, and I am hoping we can plan on having your assistance with next year''s event. It''s tentatively scheduled for January 16–0, 2023, in Tampa, Florida. If you can confirm your availability, I''ll be in touch when we''re ready to start planning.

I look forward to working with you in the future, and thank you again.

Sincerely,

William Smith
President and CEO';

    while true
    loop
     exit when length(v_txt) < 80;
     select regexp_replace(substr(v_txt, 1, 80), ' [^ ]+$', '', 1, 1)
      into v_tmp
      from dual;

     v_len := length(trim(v_tmp));
     v_out := v_out || trim(v_tmp) || chr(10);
     v_txt := substr(v_txt, v_len + 2);
    end loop;

  v_out := v_out || v_txt;
  dbms_output.put_line(v_out);
end;

This is what my code generates:

Dear Event Planning Experts Inc,

Thank you so much for your assistance in
planning our annual meeting. Your expertise in handling the meeting
arrangements, booking the conference facilities and hotel, coordinating travel,
scheduling events, and organizing the meeting is greatly appreciated.

I
appreciate your help and advice, and I am hoping we can plan on having your
assistance with next year's event. It's tentatively scheduled for January
16–20, 2023, in Tampa, Florida. If you can confirm your availability, I'll be
in touch when we're ready to start planning.

I look forward to working with
you in the future, and thank you again.

Sincerely,

William Smith
President
and CEO

Expected Result:

Dear Event Planning Experts Inc,

Thank you so much for your assistance in planning our annual meeting. Your expe
rtise in handling the meeting arrangements, booking the conference facilities a
nd hotel, coordinating travel, scheduling events, and organizing the meeting is
greatly appreciated.

I appreciate your help and advice, and I am hoping we can plan on having your a
ssistance with next year's event. It's tentatively scheduled for January 16–20,
 2023, in Tampa, Florida. If you can confirm your availability, I'll be in touc
h when we're ready to start planning.

I look forward to working with you in the future, and thank you again.

Sincerely,

William Smith
President and CEO
This post has been answered by Frank Kulash on Aug 12 2022
Jump to Answer
Comments
Post Details
Added on Aug 12 2022
6 comments
1,167 views