6.13 Regexp Demos

6.13.1 Search for regexp

The following example is a simple program that enters a regexp from the keyboard, followed by another test string, trying to find the regexp in the test string. If found, the registers are printed on the screen, and the corresponding matched substrings:

Program 6: searchDemo.bet

ORIGIN '~beta/basiclib/regexp';
--- program: descriptor ---
(#
do loop:
     (# regex: ^text;
     do 'Search for: ' -> puttext; getLine -> regex[];
        (if regex.length = 0 then leave loop if);
        loop:
          (# string: ^text;
          do 'Search for: '->puttext; regex[]->puttext;
             ' in: ' -> puttext; getline -> string[];
             (if string.length = 0 then leave loop if);
             0 -> string.pos;
             regex[] -> string.regexp_search
             (# noMatch::< (# do '\tNo match' -> putline #)
             do (for i: regexp_numberOfRegisters repeat
                     (if (i-1->regs.start)>=0 then
                         '\tregister '->puttext; i-1 -> putint; ': '->puttext;
                         i-1->regs.start->putint;
                         ' to '->puttext;
                         i-1->regs.end->putint;
                         ': '->puttext;
                         ((i-1->regs.start)+1, i-1->regs.end)->string.sub->putline
                     if)
                for);
             #);
             restart loop
          #);
        restart loop
     #)
#)

Output from the demo might be:

Search for: 1\(234\)5
Search for: 1\(234\)5 in: dfjdfhdf1235jfdkj12345ghdf
register 0: 17 to 22: 12345
register 1: 18 to 21: 234
Search for: 1\(234\)5 in: 12345
register 0: 0 to 5: 12345
register 1: 1 to 4: 234
Search for: 1\(234\)5 in: 123
No match
Search for: 1\(234\)5 in:

Search for: \(22\).*\1
Search for: \(22\).*\1 in: xxx22yyy2zzz
No match
Search for: \(22\).*\1 in: xxx22yyy22zzz
register 0: 3 to 10: 22yyy22
register 1: 3 to 5: 22

6.13.2 Replace with a regexp

The following example is nearly identical to the previous, except that it allows for the specification of a replacement string:

Program 7: replaceDemo.bet

ORIGIN '~beta/basiclib/regexp';
--- program: descriptor ---
(#
do loop:
     (# regex: ^text;
     do 'Replace: ' -> puttext; getLine -> regex[];
        (if regex.length = 0 then leave loop if);
        loop:
          (# string: ^text;
          do 'Replace: '->puttext; regex[]->puttext;
             ' in: ' -> puttext; getline -> string[];
             (if string.length = 0 then leave loop if);
             loop:
               (# t, replacement: ^text
               do string.copy -> t[]; 0 -> t.pos;
                  'Replace: '->puttext; regex[]->puttext;
                  ' in: ' -> puttext; string[]->puttext;
                  ' with: '->puttext; getline -> replacement[];
                  (if replacement.length = 0 then leave loop if);
                  (regex[], replacement[]) -> t.regexp_replace
                  (# noMatch::< (# do '\tNo match'->screen.putline #) #);
                  'Replace: '->puttext; regex[]->puttext;
                  ' in: ' -> puttext; string[]->puttext;
                  ' with: '->puttext; replacement[]->puttext;
                  ' gives: '->puttext; t[] -> putline;
                  restart loop
               #);
             restart loop
          #);
        restart loop
     #)
#)

Output from the demo might be:

Replace: [Ww]ord
Replace: [Ww]ord in: This Word is in capital
Replace: [Ww]ord in: This Word is in capital with: sentence
Replace: [Ww]ord in: This Word is in capital with: sentence gives: This sentence is in capital


Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Monday October 23rd 2000 at 11:49]