How does RegularExpression() work?

May 20, 2008
12,178
133
Syracuse, NY, USA
I'd like to get rid of my plugins dependencies on ONIG.DLL. But I can't get RegularExpression() to work. Below is my current code. I have verified that the regular expression and string are passed correctly and that the string does dontain a match for the regex. This code never finds a match.
Code:
LPWSTR RegExp(LPWSTR pRegEx, LPWSTR pString, LPDWORD pLen)
{
   LPWSTR pMatch = NULL;
   LPBYTE pstart = NULL, pend = NULL;
   UINT uRegions;
   if ( !RegularExpression( pRegEx, (LPBYTE) pString, &pstart, &pend, &uRegions, 0, 1, 0 ) && pstart != NULL )
   {
       pMatch = (LPWSTR) pstart;
       *pLen = (DWORD) (pend - pstart)/sizeof(WCHAR);
   }
   return pMatch;
}
 

rconn

Administrator
Staff member
May 14, 2008
12,557
167
This is a call inside TCMD (in the regular expression analyzer dialog):

Code:
   TCHAR szExpression[512];
   TCHAR szTest[4096];
   LPBYTE lpBegin, lpEnd;
   unsigned int nRegions;
   int fValidate = 0;

   // retrieve the expression & test values
...
...
   lpBegin = (LPBYTE)szTest;
   lpEnd = (LPBYTE)szTest;
   nRegions = 0;
   if (RegularExpression(szExpression, (LPBYTE)szTest, &lpBegin, &lpEnd, (PUINT)&nRegions, 0, 1, 1) == 0)
    fValidate = 1;
 
May 20, 2008
12,178
133
Syracuse, NY, USA
Hmmm! Here's my current code with debug Printfs.
Code:
LPWSTR RegExp(LPWSTR pRegEx, LPWSTR pString, LPDWORD pLen)
{
   Printf(L"Regex = %s\r\nString = %s\r\n", pRegEx, pString);
   LPWSTR pMatch = NULL;
   LPBYTE pstart = (LPBYTE) pString, pend = (LPBYTE) pString;
   UINT uRegions = 0;
   INT nResult = RegularExpression( pRegEx, (LPBYTE) pString, &pstart, &pend, &uRegions, 0, 1, 1 );
   if ( nResult == 0 && pstart != NULL )
   {
       Printf(L"pStart points to %s\r\n", pstart);
       pMatch = (LPWSTR) pstart;
       *pLen = (DWORD) (pend - pstart)/sizeof(WCHAR);
       Printf(L"length = %lu\r\n", *pLen);
   }
   else
       Printf(L"nResult = %d\r\npstart = %x\r\n", nResult, pstart);
   return pMatch;
}
It shows the likes of
Code:
Regex = txt
String = 2017-10-03  22:27         109,634  codb.txt
nResult = 0
pstart = 0
So RegularExpression() is finding a match (nResult = 0). But pstart doesn't point to the match; it's NULL. TakeCmd.h says "lppStart - pointer to beginning of matching string". Perhaps I'm misinterpreting that, but in any event, how do I actually find the match when there is one? (This is a console screen buffer search and I want to temporarily reverse the colors of the match.)
 
May 20, 2008
12,178
133
Syracuse, NY, USA
TakeCmd.h also says
Return != 0 for an error, but 0 can be either a match or not -- test lppStart and lppRange to check for a match
But RegularExpression() doesn't seem to be setting those pointers.

When I do the onig_search() myself, the offsets (within pString) of the beginning and end of the match are in region->beg[0] and region->end[0]. And I
Code:
*pLen = (region->end[0] - region->beg[0]) / sizeof(WCHAR);
pMatch = pString + region->beg[0] / sizeof(WCHAR);
 
May 20, 2008
12,178
133
Syracuse, NY, USA
This is a call inside TCMD (in the regular expression analyzer dialog):

Code:
TCHAR szExpression[512];
TCHAR szTest[4096];
LPBYTE lpBegin, lpEnd;
unsigned int nRegions;
int fValidate = 0;

// retrieve the expression & test values
...
...
lpBegin = (LPBYTE)szTest;
lpEnd = (LPBYTE)szTest;
nRegions = 0;
if (RegularExpression(szExpression, (LPBYTE)szTest, &lpBegin, &lpEnd, (PUINT)&nRegions, 0, 1, 1) == 0)
fValidate = 1;

That may work for thr purposes of the RE analyzer dialog, but it doesn't set nRegions, lpBegin, or lpEnd which are supposed to be "out" parameters. I did get it to set those parameters (and accomplish my goal) but to do so I had to

Code:
lpEnd = (LPBYTE) strend[szTest];