mindy regexp extensions 
Author Message
 mindy regexp extensions

-----BEGIN PGP SIGNED MESSAGE-----

Hi!

regexp-position and make-regexp-positioner don't do what us perl users
like, namely returning the matching groups as strings instead of pairs
of offsets. I've written two functions, regexp-match and
make-regexp-matcher that provide the feature. Examples:

    regexp-match("This is a string", "is");

returns "is",

    regexp-match("This is a string", "(is)(.*)ing");

returns values("is"," a str"),

    regexp-match("This is a string", "(not found)(.*)ing");

returns #f. Typical usage would be:

 let (header-name,header-value) = regexp-match(header-line,"([^:]*):\\s*(.*)");

Otherwise the functions pretty much behave like their position
counterparts.

Feedback appreciated, especially regarding coding style.

Here's the code:

define method regexp-match(big-string :: <string>, regexp :: <string>, #rest keys)
 => (#rest strings :: <string>);
  internal-string-extractor(method()
                              apply(regexp-position,big-string,regexp,keys)
                          end method,
                          big-string);
end method regexp-match;

define method make-regexp-matcher(regexp :: <string>, #rest keys)
 => positioner :: <method>;
  let positioner = apply(make-regexp-positioner,regexp,keys);
  method (big-string :: <string>)
    internal-string-extractor(method ()
                                positioner(big-string)
                            end method,
                            big-string);
  end method;
end method make-regexp-matcher;

define method internal-string-extractor(positioner :: <method>, big-string :: <string>)
 => (#rest strings :: <string>);
  block (exit)
    let (start-mark, end-mark, #rest matches) = positioner();
    if (~ start-mark) exit(#f)  end if;
    if (start-mark == #t) exit(#t);  end if;
    if (~ matches) exit(copy-sequence(big-string, start: start-mark, end: end-mark));  end if;
    let substring-list :: <deque> = make(<deque>);
    for (i :: <integer> from 0 below matches.size by 2)
      block (next)
        start-mark := matches[i];
        end-mark := matches[i + 1];
        if(~ start-mark)
          push-last(substring-list,#f);
          next();
        end if;
        push-last(substring-list,copy-sequence(big-string, start: start-mark, end: end-mark));
      end block;
    end for;
    apply(values,substring-list);
  end block;
end method internal-string-extractor;

Andreas

- --
Kallisti!

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface

iQCVAgUBMZfxN0yjTSyISdw9AQF+2AQAn0QLNODW5tyqVdKgkMLFNlNkN2NgQ2uI
d5A8TsPG1T0LdKo496lfQuI//JWd2DyMcNTfSIIGNDikW0oLFx7gE3ydCrIxT3Tn
AiCGGbIiuD1SAOgJsUAnwjUZ5kUFS/Lx4RKscsKirh45pSANH0vy9MunhTPZeBg8
TlxCoVfjI2Y=
=VTa4
-----END PGP SIGNATURE-----



Sat, 31 Oct 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Mindy future

2. Gtting started with Mindy?

3. unsigned long (4 bytes) in mindy

4. mindy 2.0 tk deadlock?, nope just a stupid mistake

5. Mindy on the BeOS

6. Mindy 2.0 for MkLinux/LinuxPPC/Rhapsody ???

7. How to install Mindy on NT4?

8. Mindy 1.5 & MacTen UNIX

9. Question about Mindy's system method

10. Mindy for Windows 95

11. Mindy for Windows 95

12. Mindy Streams/Format Libraries

 

 
Powered by phpBB® Forum Software