Thread: C & Regular Expressions

  1. #1
    Registered User
    Join Date
    Aug 2022

    Question C & Regular Expressions

    Hi, so I'm new to this site.

    I just posted a post it and already has more than 4 thousand views!

    I have a question.

    I am not well versed with PCRE2.

    In my other thread, the attached file had a gsub() function that takes three arguments.

    #define PCRE2_CODE_UNIT_WIDTH 8
    #include <pcre2.h>
    char* gsub  (const char* str, const char*     reg, const char* rep) {
    size_t outlen;
        static const char* error_str = \"DONT KNOW HOW TO ALLOCATE MEM FOR REGEX\";
        char* out = malloc(outlen =strlen(str) *      strlen(rep))  ;
        if (out == NULL) {
        fputs(error_str,   stderr);
    return NULL;
        int error;
        PCRE2_SIZE erroffset;
    //                                       printf(\"       %l                             \", 
        const PCRE2_SPTR pattern = (PCRE2_SPTR)reg;
        const PCRE2_SPTR subject = (PCRE2_SPTR)str;
        const PCRE2_SPTR replacement = (PCRE2_SPTR)rep;
        pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
        if (re == 0) return NULL;
    //        return 1;
        pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
        int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0, PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement, PCRE2_ZERO_TERMINATED, out, &outlen);
        if (rc >= 0);
    //        printf(%s\\n, output);
        size_t new_len = strlen(out);
        char* new_out = realloc(out,new_len + 1);
        if (new_out == NULL) goto error;
        return   new_out;

    The PCRE2 API is really complicated.


    1. Is there any way to have PCRE automatically account for memory allocation sizes? With a callback, I could do this manually.

    2. So is the best solution to always have a callback function?

    3. I want to implement something like Ruby's

    str.gsub /foo/ do |i|

    Could someone post a code snippet that does this?

    4. Would anyone want to contribute to my other project? (

    I can re-write it in C.

    It's just the current compiler is using gsub() functions in Crystal.

    I'm not an expert in PCRE2.

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I just posted a post it and already has more than 4 thousand views!
    It says 14 people at the bottom.
    Members who have read this thread: 14
    Besides, pressing F5 also bumps the count, so don't believe the number of "views" has any significance at all.

    The first problem you have is memory leaks whenever you error out of the code.

    > 1. Is there any way to have PCRE automatically account for memory allocation sizes?
    Well picking a large buffer, and using this seems reasonable.
    PCRE2_SUBSTITUTE_OVERFLOW_LENGTH If overflow, compute needed length
    Most of the time, you'll only do it once.
    Occasionally, you'll have to do a realloc and try again - but only once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular Expressions in gcc
    By rstanley in forum Linux Programming
    Replies: 12
    Last Post: 01-07-2016, 05:24 PM
  2. Regular expressions
    By jverkoey in forum A Brief History of
    Replies: 9
    Last Post: 01-23-2005, 09:36 PM
  3. Regular Expressions
    By Korn1699 in forum C# Programming
    Replies: 4
    Last Post: 01-12-2005, 12:50 AM
  4. regular expressions help
    By axon in forum A Brief History of
    Replies: 4
    Last Post: 09-09-2004, 07:16 PM

Tags for this Thread