简体   繁体   English

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

[英]How to extract a substring from one delimiter to another in C#?

My input is going to be as follows: abc@gmail.com,def@yahoo.com;xyz@gmail.com;ghi@hotmail.com and so on 我的意见如下:abc @ gmail.com,def @ yahoo.com; xyz@gmail.com; ghi@hotmail.com等等

Now I want my output to be: abc def xyz ghi 现在我希望我的输出为:abc def xyz ghi

The following is my code: 以下是我的代码:

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);
        }
    }
}

My output right now is: abc gmail.com def yahoo.com xyz gmail.com ghi hotmail.com 我现在的输出是:abc gmail.com def yahoo.com xyz gmail.com ghi hotmail.com

Any kind of help would be greatly appreciated. 任何形式的帮助将不胜感激。 Thanks. 谢谢。

You shouldn't use regex for split, and should no split by @ . 你不应该使用正则表达式进行拆分,也不应该用@拆分。 Instead, use the follopwing code: 相反,使用follopwing代码:

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
        }
    }
}

Edit: 编辑:

Or, with LINQ: 或者,使用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);
        }
    }
}

another approach without RegEx 没有RegEx的另一种方法

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

first Split the adresses by , and ; 首先Split地址,然后; , then select the part before the @ by splitting again. ,然后通过再次拆分选择@之前的部分。

You can use this email regex: 您可以使用此电子邮件正则表达式:

            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(" "));

As Mail is a weird thing with a complexe definition, I will never assume that something with an @ is a mail. 由于Mail是一个带有复杂定义的奇怪的东西,我永远不会认为带有@的东西是邮件。
My best try would be to convert the string to a MailAddress, just in case it look like a mail but it's not one because of some invalid char etc. 我最好的尝试是将字符串转换为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);

Actually, in the regular expression, to capture some substring, you need to wrap the expected content by ( and ) 实际上,在正则表达式中,要捕获一些子字符串,需要用()包装预期的内容

Below code should work 下面的代码应该工作

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;

The regular expression is as below 正则表达式如下

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

(.*?)@.*?[,;]{1} is fetching the substring before @ and ? (.*?)@.*?[,;]{1} ,; (.*?)@.*?[,;]{1}在@和之前获取子字符串? restrict it fetches the first match. 限制它获取第一场比赛。

The last email do not contain , or ; 最后一封电子邮件不包含,; , thus add a OR condition and fetch the last email name by the substring before @ ,因此添加OR条件并在@之前通过子字符串获取最后一个电子邮件名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM