#include <iostream>
using std::cout;
using std::endl;

#include <cstring>
using std::strcpy;
using std::strcat;

#include <cstdio>
using std::sprintf;

int main()
{
    char * lb = "(?:[a-z90](?:-?[a-z90]+)+)";
    int lbLen = strlen(lb);

    char * sbFrmat = "(?:%s(?:\\.%s){0,4})";
    int sbFrmatLen = strlen(sbFrmat);
    int sbLen = (lbLen * 2) + sbFrmatLen;

    char sb[sbLen+1];
    sprintf(sb, sbFrmat, lb, lb);
    cout << "sb\n" << sb << endl << endl;

    char * i_num = "(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])";
    int iNumLen = strlen(i_num);

    char * iFrmat = "(%s(?:\\\\.%s){3})";
    int iFrmatLen = strlen(iFrmat);
    int iLen = (iNumLen * 2) + iFrmatLen;

    char i[iNumLen + 1];
    sprintf(i, iFrmat, i_num, i_num);
    cout << "i\n" << i << endl << endl;

    char * dmFrmat = "(?:%s)";
    int dmFrmatLen = strlen(dmFrmat);
    int dmLen = lbLen + dmFrmatLen;

    char dm[dmLen + 1];
    sprintf(dm, dmFrmat, lb);
    cout << "dm\n" << dm << endl << endl;


    char tp_l[] = "([abcd]{2,4})";
    int tpLLen = strlen(tp_l);

    char p_num[]  = "(0-9]+)";
    int pNumLen = strlen(p_num);

    char * hFrmat = "((?:(?:%s\\.)?%s\\.%s|%s)(?:\\:%s)?)";
    int hFrmatLen = strlen(hFrmat);
    int hLen = sbLen + dmLen + tpLLen + iLen + pNumLen+ hFrmatLen;

    char  h[hLen + 1];
    sprintf(h, hFrmat,
                        sb,
                        dm,
                        tp_l,
                        i,
                        p_num);

    cout << "h\n" << h << endl;
}

//output

sb
(?:(?:[a-z90](?:-?[a-z90]+)+)(?:\.(?:[a-z90](?:-?[a-z90]+)+)){0,4})

i
((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])){3})

dm
(?:(?:[a-z90](?:-?[a-z90]+)+))

h
((?:(?:5[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])){3})\.)?(?:(?:[a-z90](?:-?[a-z90]+)+))\.([abcd]{2,4})|((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])){3}))(?:\:(0-9]+))?)

In the sprintf that fills "h" notice that "sb" is the first argument for formatting but sb's value "(?:(?:[a-z90)) ..." comes later on in the "h" string and the "i" value is shown twice. At the beginning of "h" the i value shows up but not completely accurate.

I'm building a regular expression, and don't understand what's going wrong.

Btw, the regexes above aren't exactly what I'm using but close enough to illustrate the point.

Also, If someone could give a brief example of how I could accomplish the concatenation that I'm doing above with C++ I'd appreciate it. I'd first like to know what's going wrong with my sprintf's though.

I've looked at "stringstream" but had problems with the stream object (if that's what you'd call it). Where it would keep the contents of the previously streamed input (stream << etc. Again not certain of the terminology) and I didn't know how to have it simply reset itself.

Thanks in advance for any information you're able to give.

Recommended Answers

All 5 Replies

The problem is classic case of buffer overrun.
char i[iNumLen + 1];
The above does not allocate enough space for the result string.

Boost libraries contain a regular expression c++ class. It might be worth tossing out this poort c implementation and using boost.

The problem is classic case of buffer overrun.
char i[iNumLen + 1];
The above does not allocate enough space for the result string.

Boost libraries contain a regular expression c++ class. It might be worth tossing out this poort c implementation and using boost.

Thanks Ancient Dragon. I looked for a long time and didn't see that. The line should read

char i[iLen + 1];

It's not exact but it provides enough space for the string.

I'll look into the boost library, but I think I still might need to build the regex string myself however.

Thanks again.

making that change and the program runs without a problem. Don't know if the output is what you want or not though.

My compiler is not c99 compilant, so I use new to allocate the arrays, but the output should be the same.

sb
(?:(?:[a-z90](?:-?[a-z90]+)+)(?:\.(?:[a-z90](?:-?[a-z90]+)+)){0,4})

i
((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[
0-9])){3})

dm
(?:(?:[a-z90](?:-?[a-z90]+)+))

h
((??:(?:(?:[a-z90](?:-?[a-z90]+)+)(?:\.(?:[a-z90](?:-?[a-z90]+)+)){0,4})\.)?(?:(
?:[a-z90](?:-?[a-z90]+)+))\.([abcd]{2,4})|((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9
])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])){3}))(?:\:(0-9]+))?)
Press any key to continue . . .

Yes, it works as it should with the change char i[iLen ...].

What I was saying was that I didn't notice the error until you pointed it out. Even after looking at the code for some time.

that one of the benefits of posting code on these boards -- often other eyes can catch errors that you can't.

commented: Nicely done. +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.