Introduction

Syntax

Program Help

Examples

Download

Match Tool 2.1

Match Tool 3.0

 

DEELX Engine Examples: Search Remark

To search a remark in a C++ program source code.


Regular Expression

/\*((?!\*/).)*(\*/)?|//([^\x0A-\x0D\\]|\\.)*


Method

Match


Code
#include "deelx.h"

int find_remark(const char * string, int & start, int & end)
{
    // declare, you can use 'static' if your regex does not change
    CRegexpT <char> regexp("/\\*((?!\\*/).)*(\\*/)?|//([^\\x0A-\\x0D\\\\]|\\\\.)*");

    // find and match
    MatchResult result = regexp.Match(string);

    // result
    if( result.IsMatched() )
    {
        start = result.GetStart();
        end   = result.GetEnd  ();
        return 1;
    }
    else
    {
        return 0;
    }
}

int main(int argc, char * argv[])
{
    char * code1 = "int a; /* a */";
    char * code2 = "int a;";

    int start, end;

    if( find_remark(code1, start, end) )
        printf("In code1, found: %.*s\n", end - start, code1 + start);
    else
        printf("In code1, not found.\n");

    if( find_remark(code2, start, end) )
        printf("In code2, found: %.*s\n", end - start, code2 + start);
    else
        printf("In code2, not found.\n");

    return 0;
}

Remarks

This example can match C style remark (/*...*/) and C++ style remark (//...).

But it does not exclude the situation that the remark is in a string constant.

 

 

RegExLab.com © 2005 - All Rights Reserved