M4BASIC - pcre.m4b
Not logged in
REM Using PCRE in M4Basic.
REM http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions
REM
REM Example taken from http://www.adp-gmbh.ch/cpp/regexp/pcre/test_1.html
REM
REM PvE, September 2014 - GPL.
REM
REM Add the necessary include files and libraries as command line option to the compiler:
REM
REM Compile method #1:
REM   # m4 basic.m4 pcre.m4b |indent > pcre.c
REM   # gcc -include pcre.h -o pcre pcre.c -lm -lgc -lpcre
REM
REM Compile method #2:
REM   # m4basic -i pcre.h -l pcre pcre.m4b
REM ----------------------------------------------------------------------------------------

CONST OVECCOUNT = 30

DECLARE error TYPE const char*
DECLARE erroffset, rc, i TYPE int
DECLARE ovector[OVECCOUNT] TYPE int
DECLARE *re TYPE pcre

CONST str = "regular expressions=very cool"

LET re = pcre_compile (
         "([^=]*)=(.*)",       /* the pattern */
         0,                    /* default options */
         &error,               /* for error message */
         &erroffset,           /* for error offset */
         NULL)                 /* use default character tables */

IF NOT(re) THEN
    PRINT erroffset, error FORMAT "pcre_compile failed (offset: %d), %s\n"
    END -1
ENDIF

LET rc = pcre_exec (
    re,                   /* the compiled pattern */
    0,                    /* no extra data - pattern was not studied */
    str,                  /* the string to match */
    LEN(str),             /* the length of the string */
    0,                    /* start at offset 0 in the subject */
    0,                    /* default options */
    ovector,              /* output vector for substring information */
    OVECCOUNT)            /* number of elements in the output vector */

IF (rc < 0) THEN
    IF rc == PCRE_ERROR_NOMATCH THEN
        PRINT "String didn't match" NL
    ELSE
        PRINT rc FORMAT "Error while matching: %d\n"
    ENDIF

    FREE re
ENDIF

FOR i = 0 TO rc-1
    PRINT i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i] FORMAT "%2d: %.*s\n"
NEXT

Return to M4BASIC