如何在文件中搜索指定字符

dachui111 2005-03-17 07:31:15
例如搜所以email并保存
...全文
334 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
linuxyf 2005-03-17
  • 打赏
  • 举报
回复
这么快就揭帖了
linuxyf 2005-03-17
  • 打赏
  • 举报
回复
用正则表达式:

1. Expression: ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-z
A-Z]\.)+[a-zA-Z]{2,9})$

Description: regex to validate email address noteworthy: (1) It allows usernames with 1 or 2 alphanum characters, or 3+ chars can have -._ in the middle. username may NOT start/end with -._ or any other non alphanumeric character. (2) It allows heirarchical ...

Matches: [e@eee.com], [eee@e-e.com], [eee@ee.eee.museum] [ More Details]

Non-Matches: [.@eee.com], [eee@e-.com], [eee@ee.eee.eeeeeeeeee]

2.
Expression: ^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Z
a-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z
A-Z]{2,6}$

Description: The most complete email validation routine I could come up with. It verifies that: - Only letters, numbers and email acceptable symbols (+, _, -, .) are allowed - No two different symbols may follow each other - Cannot begin with a symbol - Ending domain ...

Matches: [g_s+gav@com.com], [gav@gav.com], [jim@jim.c.dc.ca] [ More Details]

Non-Matches: [gs_.gs@com.com], [gav@gav.c], [jim@--c.ca]

3
Expression: ^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[
^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA
-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01
-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?
(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-
zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angl
e)>)$

Description: This accepts RFC 2822 email addresses in the form: blah@blah.com OR Blah <blah@blah.com> RFC 2822 email 'mailbox': mailbox = name-addr | addr-spec name-addr = [display-name] "<" addr-spec ">" addr-spec = local-pa ...

Matches: [name.surname@blah.com], [Name Surname <name.surname@blah.com>], ["b. blah"@blah.co.nz] [ More Details]

Non-Matches: [name surname@blah.com], [name."surname"@blah.com], [name@bla-.com]


dachui111 2005-03-17
  • 打赏
  • 举报
回复
thanks to fangxinggood(JustACoder
虽然还有点问题,但是有了很大眉目
先把贴子结了吧
dachui111 2005-03-17
  • 打赏
  • 举报
回复
文件里单单一个email到是能读出来
前后加写其他东西,就读不出来了
机器人 2005-03-17
  • 打赏
  • 举报
回复
没点规律肯定不行啊。
提供点思路:首先用FileStream和StreamReader把文件读进一个string里。再通过正则表达式过滤。
System.IO.FileStream fs = new System.IO.FileStream(filePath,System.IO.FileMode.Open,System.IO.FileAccess.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);
string str = sr.ReadToEnd();
System.Text.RegularExpressions.Regex mailRegex = new System.Text.RegularExpressions.Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
System.Text.RegularExpressions.MatchCollection mc = mailRegex.Matches(str);
for(int i = 0; i<mc.Count; i++ )
Console.WriteLine( mc[i].ToString() );
dazhu2 2005-03-17
  • 打赏
  • 举报
回复
可以用正则表达式://正则表达式
string s_reg=@"^[a-zA-Z]{1}[a-zA-Z_0-9]*@[a-zA-Z\.0-9]+$";
AllenTing 2005-03-17
  • 打赏
  • 举报
回复
正则:
email的:
w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+
更多:
http://www.mscenter.edu.cn/blog/yongsheng/archive/2004/11/19/308.aspx
dachui111 2005-03-17
  • 打赏
  • 举报
回复
在普通的文本文件中搜索xxx@xx.com好像要用到正则表达式
Z8SOFT 2005-03-17
  • 打赏
  • 举报
回复
string Strsql="select * from XXX where keyword ='"+email+"'"

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧