Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Perl regular expression library

Perl regular expression library

From: David Fetter <dfetter_at_shell4.ba.best.com>
Date: 14 Nov 1999 08:43:55 GMT
Message-ID: <382e764b$0$241@nntp1.ba.best.com>


Kind people,

I'm trying to write an abbreviated regular expression library for use in Oracle (8.0.5.1 for Linux). The features I'd like to put in it are regex match and regex substitution.

Unfortunately, I haven't been able to get the shared library to compile.

Big thanks in advance for any tips, hints or tricks.

Here follows the C code and the PL/SQL code I've written so far. Please don't laugh too hard :)

--begin C--

#include <stdio.h>
#include <time.h>
#include <oci.h>
#include <EXTERN.h>
#include <perl.h>

SV* my_perl_eval_sv(SV *sv, I32 croak_on_error) {

    dSP;
    SV* retval;
    STRLEN n_a;

    PUSHMARK(SP);
    perl_eval_sv(sv, G_SCALAR);

    SPAGAIN;
    retval = POPs;
    PUTBACK;     if (croak_on_error && SvTRUE(ERRSV))

       croak(SvPVx(ERRSV, n_a));

    return retval;
}

/** match(string, pattern)

**

I32 match(SV *string, char *pattern)
{

    SV *command = NEWSV(1099, 0), *retval;     STRLEN n_a;

    sv_setpvf(command, "my $string = '%s'; $string =~ %s",

             SvPV(string,n_a), pattern);

    retval = my_perl_eval_sv(command, TRUE);     SvREFCNT_dec(command);

    return SvIV(retval);
}

/** substitute(string, pattern)

**

I32 substitute(SV **string, char *pattern) {

    SV *command = NEWSV(1099, 0), *retval;     STRLEN n_a;

    sv_setpvf(command, "$string = '%s'; ($string =~ %s)",

             SvPV(*string,n_a), pattern);

    retval = my_perl_eval_sv(command, TRUE);     SvREFCNT_dec(command);

    *string = perl_get_sv("string", FALSE);     return SvIV(retval);
}

/** matches(string, pattern, matches)

**

I32 matches(SV *string, char *pattern, AV **match_list) {

    SV *command = NEWSV(1099, 0);
    I32 num_matches;
    STRLEN n_a;

    sv_setpvf(command, "my $string = '%s'; @array = ($string =~ %s)",

             SvPV(string,n_a), pattern);

    my_perl_eval_sv(command, TRUE);
    SvREFCNT_dec(command);

    *match_list = perl_get_av("array", FALSE);     num_matches = av_len(*match_list) + 1; /** assume $[ is 0 **/

    return num_matches;
}
--end C--
--begin PL/SQL--
CREATE OR REPLACE LIBRARY regex_l
AS

    '/path/to/libregex.so';
/

CREATE OR REPLACE PACKAGE regex
IS

    FUNCTION regex_match (

        string_in    IN VARCHAR2,
        regex_in     IN VARCHAR2

    )
    RETURN PLS_INTEGER;
    PRAGMA RESTRICT_REFERENCES (send_mail, WNDS, RNDS, WNPS, RNPS);

    FUNCTION regex_substitute (

        string_in    IN VARCHAR2,
        regex_in     IN VARCHAR2

    )
    RETURN VARCHAR2;
    PRAGMA RESTRICT_REFERENCES (send_mail, WNDS, RNDS, WNPS, RNPS);

END regex;
/

CREATE OR REPLACE PACKAGE BODY regex
IS

    FUNCTION regex_match (

        string_in    IN CHAR,
        regex_in     IN CHAR

    )
    RETURN PLS_INTEGER
    IS EXTERNAL
        LIBRARY regex_l
        NAME "match"
        LANGUAGE C
        PARAMETERS
            (string_in STRING,
             regex_in  STRING
             RETURN integer);

    FUNCTION regex_substitute (
        string_in    IN CHAR,
        regex_in     IN CHAR

    )
    RETURN PLS_INTEGER
    IS EXTERNAL
        LIBRARY regex_l
        NAME "substitute"
        LANGUAGE C
        PARAMETERS
            (string_in STRING,
             regex_in  STRING
             RETURN integer);

END regex;
/
--end PL/SQL--

--

            David Fetter         888 O'Farrell Street Apt E1205
       dfetter_at_best.com           San Francisco, CA 94109-7089 USA
  http://www.best.com/~dfetter     +1 415 567 2690 (voice)
print unpack ("u*",q+92G5S="!!;F]T:&5R(%!E<FP_at_2&%C:V5R"@``+)

Why is it that when we talk to God we're said to be praying, but when God talks to us we're schizophrenic?

                                       Lily Tomlin
Received on Sun Nov 14 1999 - 02:43:55 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US