很想实现类似 php函数里的 ereg 功能.欢迎探讨
/*
* 从文件里得到邮件地址 2003-2-8 17:15
*/
#include <stdlib.h>
#include <stdio.h>
#define mailMaxLen 20
#define bufMaxLen 200
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
int main(int argc,char *argv[])
{
FILE *fp,*fp_write;
int i,temp_len;
int bool_find=0,bool_finished=0;
long file_len=0;
char fileName[30],write_file[30];
char mail_data[50]; //保存得到的临时邮件
char *buf;
char *p,*mail_begin,*temp_p;
if(argc!=2)
{ printf("error!\nExp: %s yourmail.dat\n",argv[0]);
return 0;
}
sprintf(write_file, "tmp_%s", argv[1]); //生成要写入的文件名
strcpy(fileName, argv[1]);
if((fp=fopen(fileName,"rb"))==NULL)
{ printf("\nCan't open The file for %s to read! \n",fileName);
printf("Press any key to halt!");
return(0);
}
if((fp_write=fopen(write_file,"w"))==NULL)
{ printf("\nCan't open The file for %s to write! \n",fp_write);
printf("Press any key to halt!");
return(0);
}
file_len=filesize(fp);
buf=(char *)malloc(file_len);
fread(buf,file_len,1, fp);
for(p=buf;*p!='\0';p++)
{
// if(*p=='_' || *p=='.' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') )
if(*p=='@')
{
bool_finished=0;
bool_find=0;
mail_begin=p;
temp_len=0;
while(temp_len<mailMaxLen && p!=buf)//后退,找到邮箱名
{
p--;
//如果符合标准的
if(*p=='_' || *p=='-' || *p=='.' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
{
temp_len++;
}
else
{
break;
}
}
p++;//指向正确的开始
//eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email))
if(mail_begin!=p) //符合条件了
{
temp_p=mail_begin; //这时temp_p 指向@处
mail_begin=p; //记录邮件名开始地址
p=++temp_p; //p指向@后的第一个字符
// printf("%s %c","ok",*temp_p);
//接下来的第1个字符应该是a-z 0-9 之间
while((*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') && *p!='\0')
{
p++;
//紧接着应该是a-z -
if(*p=='-' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
{ p++;
}
else
{ bool_find=0;
break;
}
temp_len=0; //[email protected]
while(temp_len<mailMaxLen && *p!='\0')
{ if(*p=='-' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
{ p++;
temp_len++;
}//
else if(*p=='.')//找到点.了 +[a-z]{2,3}$
{ bool_find++;//找到一段了
temp_p=p; //temp_p 指向.所在的位置
p++;
break;
}
else
{ bool_finished=1; //退出循环
break;
}
}//while(temp_len<mailMaxLen && *p!='\0')
if(bool_finished==1 && bool_find==0)//不符合
break;
}//while
}// if(mail_begin!=p) //符合条件了
// [email protected] [email protected]
if(bool_find>0)
{
p=temp_p; //p退到最后一个.的位置
p=p+3;
if(!((*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))) //最后的 .net 不符合
{ p--;
}
/*
for(temp_p=mail_begin;temp_p<=p;temp_p++)
{ printf("%c",*temp_p);
}
*/
// strncpy(mail_data, mail_begin, 3);
fwrite(mail_begin, p-mail_begin+1, 1, fp_write);
fputc('\n',fp_write);
// printf("%c",'\n');
*p=' '; //这样做是为了下次检查时从此开始
}
}// if(*p=='@')
// printf("%c",*p);
}//for
fclose(fp);
fclose(fp_write);
free(buf);
printf("\n--------------------\nok!");
printf("\nfile writed to '%s'!",write_file);
return(0);
}
本文地址:http://com.8s8s.com/it/it1955.htm