所以,
我有一个包含多个电子邮件地址的大型txt文件,而某些电子邮件地址在单词" com"之前没有点。现在我想在保存到文件之前在所有invalids电子邮件地址中添加此字符串。
例如,某些电子邮件地址采用以下格式:
aaglt_assessoria@yahoocom.br
正确的方法应该是:
aaglt_assessoria@yahoo.com.br
以及关于我如何保存这些电子邮件的代码:
function ExtractEmails(const Input: String; out Emails: TStringList): Boolean;
var
I, position : Integer;
Buffer : String;
Tokens : TStringList;
begin
Result := False;
Tokens := TStringList.Create;
Emails := TStringList.Create;
Try
Tokens.Delimiter := #32;
Tokens.DelimitedText := Input;
For I := 0 To Tokens.Count - 1 Do
begin
Buffer := Tokens.Strings[I];
If Length(Buffer) < 5 Then Continue;
If (Pos('@',Buffer) > 1) And
(Pos('.',Buffer) > 3) And
(Pos('.',Buffer) < Length(Buffer)) Then
begin
Emails.Add(Buffer);
end;
end;
Result := True;
Finally
Tokens.Free;
end;
end;
procedure EscreveEmails;
var
Linhas,Emails:TStringList;
arq: TextFile;
begin
Linhas := TStringList.Create;
Emails := TStringList.Create;
try
Linhas.LoadFromFile('foo.txt');
AssignFile(arq, 'new.txt');
Rewrite(arq);
If ExtractEmails(Linhas.Text,Emails) Then
begin
Writeln(arq,Emails.Text);
end;
finally
CloseFile(arq);
Linhas.Free;
Emails.Free;
end;
end;
答案 0 :(得分:3)
也许你可以使用正则表达式。我不是一个正则表达式的破解,但这就是我的意思:
https://regex101.com/r/kX8yQ4/2
uses
System.RegularExpressions;
function repairMailDot(const email: string): string; inline;
begin
result := TRegEx.Replace(email, '(@.*(?<!\.))(com(?:$|\..+))', '\1.\2', [roIgnoreCase])
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(repairMailDot('aaglt_assessoria@yahoocom.br'));
ShowMessage(repairMailDot('aaglt_assessoria@yahoo.com.br'));
ShowMessage(repairMailDot('aaglt_assessoria@yahoocom'));
ShowMessage(repairMailDot('aaglt_assessoria@yahoo.com'));
end;
答案 1 :(得分:0)
我会先说测试一下电子邮件地址是否是有效的电子邮件地址
function MailURLMayBeInvalid(const s: string): Boolean;
var
i: Integer;
c: string;
begin // ' ', ä, ö, ü, ß, [, ], (, ), : in EMail-Address
Result := (Trim(s) = '') or (Pos(' ', AnsiLowerCase(s)) > 0) or
(Pos('ä', AnsiLowerCase(s)) > 0) or (Pos('ö', AnsiLowerCase(s)) > 0) or
(Pos('ü', AnsiLowerCase(s)) > 0) or (Pos('ß', AnsiLowerCase(s)) > 0) or
(Pos('[', AnsiLowerCase(s)) > 0) or (Pos(']', AnsiLowerCase(s)) > 0) or
(Pos('(', AnsiLowerCase(s)) > 0) or (Pos(')', AnsiLowerCase(s)) > 0) or
(Pos(':', AnsiLowerCase(s)) > 0);
if Result then Exit; // @ not in EMail-Address;
i := Pos('@', s);
Result := (i = 0) or (i = 1) or (i = Length(s));
if Result then Exit;
Result := (Pos('@', Copy(s, i + 1, Length(s) - 1)) > 0);
if Result then Exit; // Domain <= 1
c := Copy(s, i + 1, Length(s));
Result := Length(c) <= 1;
if Result then Exit;
i := Pos('.', c);
Result := (i = 0) or (i = 1) or (i = Length(c));
end;
如果它不是一个有效的地址,那么你可以简单地检查字符串,看看你可以用LastDelimiter
函数执行的com的最后一次出现,这个函数应该可以解决这个问题
function returnAddress(Address: String): String;
begin
insert('.', Address, LastDelimiter('com',Address)-2);
Result := Address;
end;