NO

Author Topic: Help with Regex  (Read 5516 times)

yingfan

  • Guest
Help with Regex
« on: February 12, 2007, 02:27:18 AM »
I am trying to write a simple program to download the latest sysclean and the relevant virus pattern from Trend Micro's website. The first regex I trying to match is the md5 hash, but having trouble to match it.

The code is as follow:

Code: [Select]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

int main()
{
char filename[100]  , buf[4096]  , output[102400];
_regmatch_t  matches;
_regex_t regex;
FILE *pipe;

strcpy(buf , "[:alnum:]{32}");
_regcomp(&regex , buf , _REG_EXTENDED);

strcpy(filename , "c:\\tools\\curl\\curl.exe -s http://www.trendmicro.com/download/dcs.asp");
pipe = _popen(filename , "r");
while (fgets(buf , 4096 , pipe) != NULL)
{
strncat(output , buf , 4096);
}
_pclose(pipe);

_regexec(&regex , output , 1 , &matches , 0);
strncpy(buf , output + matches.rm_so , matches.rm_eo - matches.rm_so);
buf[matches.rm_eo - matches.rm_so] = '\0';
printf("//%s//%d//%d//" , buf , matches.rm_so , matches.rm_eo);


return 0;
}


I can't match the md5 using [:alnum:]{32}, but is able to match if I use hardcode (46d289a5c4b0c835e5624a7be840cdeb). Can anyone point out where the problem is ?

By the way, I am able to finish a similiar script in PHP in minutes, but have worked for days on C code #-o

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Help with Regex
« Reply #1 on: February 12, 2007, 07:00:46 PM »
Quote from: "yingfan"

I can't match the md5 using [:alnum:]{32}, but is able to match if I use hardcode (46d289a5c4b0c835e5624a7be840cdeb). Can anyone point out where the problem is ?


Replace [:alnum:]{32} with either one of these:

Code: [Select]
[[:alnum:]]{32}
Code: [Select]
[a-fA-F0-9]{32}

Quote from: "yingfan"

By the way, I am able to finish a similiar script in PHP in minutes, but have worked for days on C code #-o

It would be the other way around for me, I guess...  :wink:
/Pelle

yingfan

  • Guest
Help with Regex
« Reply #2 on: February 13, 2007, 02:13:17 AM »
Thanks, Pelle!
Seems like I substituted the PCRE's way of doing (\w+) into C's regex  :wink:
Bluntly missed the [a-fA-F0-9] though, haha

Will update here when I finish the program. Just FYI, It will be running on my company's win2003 server as a schedule task. Then the workstations will download the sysclean package from the server when needed. I guess sysclean is the only free for commercial on spyware removal. That's why I need it.[/b]