如何用正则表达式从一个文本中提取所有的email地址?
代码应该怎样写?
我也想知道,我留意这个问题都很久了,以下是一些DFW的回答:
问题:如何从文件中提取含EMail地址呢 ( 积分:100, 回复:9, 阅读:188 )
分类:局域网 / 通讯 ( 版主:卷起千堆雪tyn, cAkk )
来自:goddy, 时间:2002-8-13 15:50:00, ID:1261606 [显示:小字体 | 大字体]
如何从文件中提取含EMail地址呢
来自:naughtboy, 时间:2002-8-13 15:57:00, ID:1261618
查找"@"字符,而且email地址和其他文字只见一定要隔开,否则呵呵..
比如 naughtboy@163.com一定要和后面的文字分开
来自:cxz9, 时间:2002-8-13 17:55:00, ID:1261993
用正则表达式呀
[_a-zA-Z/d/-/.]+@[_a-zA-Z/d/-]+(/.[_a-zA-Z/d/-]+)+
来自:哈利波特, 时间:2002-8-13 18:31:00, ID:1262119
应该要分开吧
来自:天与地, 时间:2002-8-13 18:34:00, ID:1262128
从文档里分离,有什么好办法吗?估计挺烦的。
能不能从设计思路上调整一下,不让Mail混在文档里面?
来自:goddy, 时间:2002-8-20 14:35:00, ID:1275966
我自己搞定了﹐大家有什么好建意﹐提一提
#include <stdio.h>
String GetEmail(String FileName)
{
FILE *in, *in1;
char readchar;
char readchar1;
String EMailAddr="";
String StrEMailAddr="";
if ((in = fopen(FileName.c_str(), "rb"))== NULL)
return "";
if ((in1 = fopen(FileName.c_str(), "rb"))== NULL)
return "";
fseek(in, 0L, SEEK_END);
int length= ftell(in);
fseek(in, 0L, SEEK_SET);
fseek(in1, 0L, SEEK_SET);
for( int i=0;i<length;i++)
{
fread(&readchar,sizeof(readchar),1,in);
if(readchar=='@')
{
fseek(in1, i-1, SEEK_SET);
fread(&readchar1,sizeof(readchar1),1,in1);
int j=2;
while((readchar1>='0'&&readchar1<='9')||(readchar1>='a'&&readchar1<='z')||(readchar1>='A'&&readchar1<='Z')||readchar1=='_'||readchar1=='.')
{
if((i-j)<=0)
break;
fseek(in1, i-j, SEEK_SET);
j++;
fread(&readchar1,sizeof(readchar1),1,in1);
}
if(j>3)
{
fread(&readchar1,sizeof(readchar1),1,in1);
while((readchar1>='0'&&readchar1<='9')||(readchar1>='a'&&readchar1<='z')||(readchar1>='A'&&readchar1<='Z')||readchar1=='_'||readchar1=='.'||readchar1=='@')
{
if((readchar1>='0'&&readchar1<='9')||(readchar1>='a'&&readchar1<='z')||(readchar1>='A'&&readchar1<='Z')||readchar1=='_'||readchar1=='.'||readchar1=='@')
EMailAddr+=AnsiString(readchar1);
fread(&readchar1,sizeof(readchar1),1,in1);
}
if(EMailAddr.Length()>7&&EMailAddr[EMailAddr.Length()]!='@'&&EMailAddr[EMailAddr.Length()]!='.')
StrEMailAddr+=","+ EMailAddr;
EMailAddr="";
}
}
}
fclose(in);
fclose(in1);
return StrEMailAddr;
}
=========================================================
来自:飞来石, 时间:2002-8-20 17:13:00, ID:1276353
function IsEMail(EMail: String): Boolean;
var
s: String;
ETpos: Integer;
begin
ETpos:= pos('@'
EMail);
if ETpos > 1 then
begin
s:= copy(EMail
ETpos+1
Length(EMail));
if (pos('.'
s) > 1) and (pos('.'
s) < length(s)) then
Result:= true else
Result:= false;
end
else
Result:= false;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if isemail(Edit1.Text) then
begin
ShowMessage('eMail-Adresse!');
end;
end;