我现在的做法 void DoMatch(String s) { MatchCollection mc; String[] results = new String[20]; int[] matchposition = new int[20];
// Create a new Regex object and define the regular expression. Regex r = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); // Use the Matches method to find all matches in the input string. mc = r.Matches(s); // Loop through the match collection to retrieve all // matches and positions. for (int i = 0; i < mc.Count; i++) { // Add the match string to the string array. results[i] = mc[i].Value; // Record the character position where the match was found. matchposition[i] = mc[i].Index; Console.WriteLine( results[i] ); } }
for your purpose, you could just do string s = "..."; Regex re = new Regex(@"(?<email>[^@\s:]+@\S+)\s*", RegexOptions.IgnoreCase); foreach (Match m in re.Matches(s)) Console.WriteLine(m.Groups["email"].Value);
sorry, the trailing \s* is not needed@"(?<email>[^@\s:]+@\S+)"
OK. It works good. Althouth not perfect. Thank u very much~
Why it is not perfect it is just because the Chines char : eg: 电子邮件:[email protected] will get [email protected] ------- Never mind ,I replace the source txt file
void DoMatch(String s)
{
MatchCollection mc;
String[] results = new String[20];
int[] matchposition = new int[20];
// Create a new Regex object and define the regular expression.
Regex r = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
// Use the Matches method to find all matches in the input string.
mc = r.Matches(s);
// Loop through the match collection to retrieve all
// matches and positions.
for (int i = 0; i < mc.Count; i++)
{
// Add the match string to the string array.
results[i] = mc[i].Value;
// Record the character position where the match was found.
matchposition[i] = mc[i].Index;
Console.WriteLine( results[i] );
}
}
思路:
从文本文件读取每行;
从每行中参考正则表达式,提取合条件的电子邮箱;
输出提取的电子邮箱
-----------
以上程序用的正则表达式是一个用于判断电子邮箱格式,而不是提取的表达式。所以只对电子邮箱地址在单独一行的有效。像mailto:[email protected]这样格式的就读取不出来了。
Console.WriteLine(m.Groups["email"].Value);
Thank u very much~
eg: 电子邮件:[email protected]
will get
[email protected]
-------
Never mind ,I replace the source txt file