using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
class Program
{
static void Main()
{
string data = "Name: John, Age: 30, Email: john@example.com; Name: Jane, Age: 25, Email: jane@example.com";
List<Person> people = new List<Person>();
string pattern = @"Name: (\w+), Age: (\d+), Email: (\S+)";
MatchCollection matches = Regex.Matches(data, pattern);
foreach (Match match in matches)
{
Person person = new Person
{
Name = match.Groups[1].Value,
Age = int.Parse(match.Groups[2].Value),
Email = match.Groups[3].Value
};
if (!IsValidEmail(person.Email))
{
Console.WriteLine($"Invalid email for {person.Name}: {person.Email}");
}
else
{
people.Add(person);
}
}
// 可以在这里对people列表进行进一步操作
}
static bool IsValidEmail(string email)
{
string emailPattern = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
return Regex.IsMatch(email, emailPattern);
}
}