[向高手请教] 取字符串问题困扰很久。 本帖最后由 subxli 于 2010-06-12 17:00:41 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>"+ "To: <[email protected]>"+ "Cc: 'yy'"+ "Subject: oo";如果你的From: =?gb2312?B?ztK+zcrHyfE=?= 这一节是死的,那么你可以用sb.indexOf("From: =?gb2312?B?ztK+zcrHyfE=?= <")获取一个值假设为xx。让后sb.subString(xx,17)来获取[email protected]。获取[email protected] 也可以这样实现!希望对你有帮助 string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" + "To: <[email protected]>" + "Cc: 'yy'" + "Subject: oo"; MatchCollection s = Regex.Matches(sb, @"(?<EmailStr>\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)", RegexOptions.IgnoreCase); foreach (Match m in s) { Console.WriteLine(m.Value); } Console.Read(); string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" + "To: <[email protected]>" + "Cc: 'yy'" + "Subject: oo"; string send = sb.Split('<', '>')[1];string forms = sb.Split('<', '>')[3]; string send = sb.Substring(sb.IndexOf("<",0) + 1, sb.IndexOf(">") - sb.IndexOf("<",0)-1);string froms = sb.Substring(sb.IndexOf("<", sb.IndexOf("<", 0) + 1) + 1, sb.IndexOf(">", 0) - sb.IndexOf("<", 0)); string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>"+ "To: <[email protected]>"+ "Cc: 'yy'"+ "Subject: oo";可以用replace()剩余 <A> to: <B>string send = sb.Split('to:')[0];....... 先找到"From:",剪了。再找到"To:"的index,把之前的串读出来,剪了。 再找到"Cc:"的index,把之前的串读出来,剪了。 以此类推。比较笨的方法。 private static void TestRegex31(){ string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" + "To: <[email protected]>" + "Cc: 'yy'" + "Subject: oo"; MatchCollection mc = Regex.Matches(sb, @"(?<=<)[^>]+"); string send = mc[0].Value; //需要取到 [email protected] string froms = mc[1].Value; //需要取到 [email protected] } 请问下 Regex.Matches(sb, @"(?<=<)[^>]+"); 是什么意思?特别是"(?<=<)[^>]+"这里 正则表达式找到<开始,不匹配<,吧后面的非>的内容匹配出来。测试的结果如何。 测试是正确的啊 ,可是我不保证只有<>这个,万一它字符串后面又有个呢 ,如 : string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" + "To: <[email protected]>" + "Cc: 'yy'" + "Subject: oo" + "content : <[email protected]>"; private static void TestRegex31(){ string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" + "To: <[email protected]>" + "Cc: 'yy'" + "Subject: oo"; Match m = Regex.Match(sb, @"(?i)from[^<]*<(?<from>[^>]+).+?to[^<]*<(?<send>[^>]+)"); string send = m.Groups["from"].Value; //需要取到 [email protected] string froms = m.Groups["send"].Value; //需要取到 [email protected] Console.WriteLine("send:" + send); Console.WriteLine("from:" + froms);} 这样就可以确定是From: 里 和 To: 里的 邮箱了吗? 高手来看下了。 .Net Hashtable 关于其GetHashCode()值的疑问... 诚心请教:这段从dataset中读取表内容的代码问题出在何处? 客户端回发后Web页中的Repeater消失了? GDI+里关于显示和取消显示图片的问题 排序显示的问题 散分,散分,散分,,,,升猩猩了散分ING!~~ UP的有分,,,欢迎光临!!~~~ 如何制作下栽 求助:我该学什么好?&散分 事务问题 c# 如何打开mp3文件 windows下的webBrowser
"To: <[email protected]>"+
"Cc: 'yy'"+
"Subject: oo";
如果你的From: =?gb2312?B?ztK+zcrHyfE=?= 这一节是死的,那么你可以用
sb.indexOf("From: =?gb2312?B?ztK+zcrHyfE=?= <")获取一个值假设为xx。
让后sb.subString(xx,17)来获取[email protected]。获取[email protected] 也可以这样实现!
希望对你有帮助
"To: <[email protected]>" +
"Cc: 'yy'" +
"Subject: oo"; MatchCollection s = Regex.Matches(sb, @"(?<EmailStr>\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)", RegexOptions.IgnoreCase);
foreach (Match m in s)
{
Console.WriteLine(m.Value);
}
Console.Read();
"To: <[email protected]>" +
"Cc: 'yy'" +
"Subject: oo";
string send = sb.Split('<', '>')[1];
string forms = sb.Split('<', '>')[3];
string froms = sb.Substring(sb.IndexOf("<", sb.IndexOf("<", 0) + 1) + 1, sb.IndexOf(">", 0) - sb.IndexOf("<", 0));
"To: <[email protected]>"+
"Cc: 'yy'"+
"Subject: oo";可以用replace()
剩余 <A> to: <B>string send = sb.Split('to:')[0];
.......
再找到"To:"的index,把之前的串读出来,剪了。
再找到"Cc:"的index,把之前的串读出来,剪了。
以此类推。比较笨的方法。
{
string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" +
"To: <[email protected]>" +
"Cc: 'yy'" +
"Subject: oo";
MatchCollection mc = Regex.Matches(sb, @"(?<=<)[^>]+");
string send = mc[0].Value; //需要取到 [email protected]
string froms = mc[1].Value; //需要取到 [email protected]
}
请问下 Regex.Matches(sb, @"(?<=<)[^>]+"); 是什么意思?特别是"(?<=<)[^>]+"这里
找到<开始,不匹配<,吧后面的非>的内容匹配出来。测试的结果如何。
测试是正确的啊 ,可是我不保证只有<>这个,万一它字符串后面又有个呢 ,如 :
string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" +
"To: <[email protected]>" +
"Cc: 'yy'" +
"Subject: oo" +
"content : <[email protected]>";
{
string sb = "From: =?gb2312?B?ztK+zcrHyfE=?= <[email protected]>" +
"To: <[email protected]>" +
"Cc: 'yy'" +
"Subject: oo";
Match m = Regex.Match(sb, @"(?i)from[^<]*<(?<from>[^>]+).+?to[^<]*<(?<send>[^>]+)");
string send = m.Groups["from"].Value; //需要取到 [email protected]
string froms = m.Groups["send"].Value; //需要取到 [email protected]
Console.WriteLine("send:" + send);
Console.WriteLine("from:" + froms);
}
这样就可以确定是From: 里 和 To: 里的 邮箱了吗?