欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET读取所有目录下文件正则匹配文本电子邮件

程序员文章站 2022-10-26 20:53:42
复制代码 代码如下:using system; using system.collections.generic; using system.text; using sys...
复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.io;
using system.text.regularexpressions;
namespace test
{
class fiemail
{
public static void main(string[] args) {
console.writeline("请输入内容路径:");
string[] fs=directory.getfiles(args[0]);
if(fs!=null){
string jobspattern=@"([\w-]+(\.\w+)*@([\w-]+\.)+\w{2,3})";
regex rx = new regex(jobspattern,regexoptions.compiled | regexoptions.ignorecase | regexoptions.multiline );
foreach(string s in fs){
console.writeline("匹配");
string asr=file.readalltext(s);
matchcollection matches = rx.matches(asr);
if(matches!=null && matches.count>0) {
string argsp="";
for(int loopi=0;loopi<matches.count;loopi++){
argsp+=(matches[loopi].groups[0].value)+environment.newline ;
}
w(argsp.trim(new char[]{'\n'}));
}
console.writeline(s);
}
}
console.readline();
}
public static void w(string ids){
string paths=system.io.path.combine(@"c:\documents and settings\administrator\桌面\gc\","edel.txt");
using (system.io.streamwriter sr = new system.io.streamwriter(paths,true)){
sr.writeline(ids);
}
}
}
}

然后csc编译,生成f.exe文件。然后就可以使用了。
里面牵扯到的技术要点:
1: c# 读取文件夹下所有文件
2:c# 利用正则获取文件中的邮址
3:c#文件写入。
完毕。