如何在C#中从一个分隔符提取子字符串到另一个



我的输入如下:abc@gmail.com,def@yahoo.com;xyz@gmail.com;ghi@hotmail.com等等

现在我希望我的输出是:abcdefxyzghi

以下是我的代码:

using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main(string[] args)
{
string str;
string[] newstr,newstr2;
Console.WriteLine("Enter the email addresses: ");
str=Console.ReadLine();
newstr=Regex.Split(str,",|;|@");
foreach (string s in newstr)
{
Console.WriteLine(s);
}
}
}

我现在的输出是:abcgmail.comdef雅虎xyzgmail.comghihotmail.com

任何形式的帮助都将不胜感激。谢谢

您不应该使用regex进行拆分,也不应该使用@进行拆分。相反,使用以下代码:

using System;
public class Program
{
public static void Main(string[] args)
{
string str;
string[] newstr;
Console.WriteLine("Enter the email addresses: ");
str = Console.ReadLine();
newstr = str.Split(new char[] { ',', ';' }); // Split to get a temporal array of addresses 
foreach (string s in newstr)
{
Console.WriteLine(s.Substring(0, s.IndexOf('@'))); // Extract the sender from the email addresses
}
}
}

编辑:

或者,使用LINQ:

using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
string str;
string[] newstr;
Console.WriteLine("Enter the email addresses: ");
str = Console.ReadLine();
newstr = str.Split(new char[] { ',', ';' })  // Split to get a array of addresses to work with
.Select(s => s.Substring(0, s.IndexOf('@'))).ToArray(); // Extract the sender from the email addresses
foreach (string s in newstr)
{
Console.WriteLine(s);
}
}
}

另一种没有RegEx 的方法

string input = "abc@gmail.com,def@yahoo.com;xy@gmail.com; ghi@hotmail.com";
var result = input.Split(',', ';').Select(x => x.Split('@').First());

首先Split,;地址,然后通过再次拆分选择@之前的部分。

您可以使用此电子邮件正则表达式:

var regex = new Regex(@"(?<name>w+([-+.']w+)*)@w+([-.]w+)*.w+([-.]w+)*");
var results =
regex.Matches("abc@gmail.com,def@yahoo.com;xyz@gmail.com;ghi@hotmail.com")
.Cast<Match>()
.Select(m => m.Groups["name"].Value)
.ToList();

也许使用它可以帮助

str.Substring(0, str.LastIndexOf(" ")<0?0:str.LastIndexOf(" "));

由于邮件是一个定义复杂的奇怪事物,我永远不会认为带有@的东西就是邮件
我最好的尝试是将字符串转换为MailAddress,以防它看起来像一封邮件,但由于某些无效字符等原因而不是一封邮件。

string input = "abc@gmail.com,ghi@hotmail.com;notme; @op this is not a mail!";
var result = input
.Split(',', ';')    // Split
.Select(x =>
{
string adr = "";
try
{   // Create an MailAddress, MailAddress has no TryParse.
adr = new MailAddress(x).User;
}
catch
{
return new { isValid = false, mail = adr };
}
return new { isValid = true, mail = adr };
})
.Where(x => x.isValid)
.Select(x => x.mail);

实际上,在正则表达式中,要捕获一些子字符串,需要用()包装期望的内容

以下代码应工作

string str22 = "abc@gmail.com;def@yahoo.com,xyz@gmail.com;fah@yao.com,h347.2162@yahoo.com.hk";// ghi@hotmail.com";
List<string> ret = new List<string>();
string regExp = @"(.*?)@.*?[,;]{1}|(.*)@";
MatchCollection matches = Regex.Matches(str22, regExp, RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
if (match.Success)
{
int pvt = 1;
while (string.IsNullOrEmpty(match.Groups[pvt].Value))
{
pvt++;
}
MessageBox.Show(match.Groups[pvt].Value);
}
}
return;

正则表达式如下

(.*?)@.*?[,;]{1}|(.*)@

(.*?)@.*?[,;]{1}在@和?限制它获取第一个匹配之前获取子字符串。

最后一封电子邮件不包含,;,因此添加or条件,并通过@之前的子字符串获取最后一封邮件名称