using System.Text.RegularExpressions;
public static void Main()
string Name = "ИП Горбатовский Е. Г.";
string FullName = "ИП Горбатовский Евгений Геннадьевич";
Console.WriteLine(GetNames(Name, FullName).Lastname + " " + GetNames(Name, FullName).Firstname);
public static (string Firstname, string Lastname) GetNames(string dbName, string dbFullName)
(string Firstname, string Lastname) result = (string.Empty, string.Empty);
string Firstname = string.Empty;
string Surname = string.Empty;
string tempStrResult = string.Empty;
string pattern = @"(?:(?:\p{Lu}\p{Ll}+[ \t]){2}\p{Lu}\p{Ll}+|(?:\p{Lu}+[ \t]){2}\p{Lu}+)";
Regex rg = new Regex(pattern);
if (!string.IsNullOrEmpty(dbName))
MatchCollection matchesName = rg.Matches(dbName);
if (matchesName.Count > 0)
foreach (Match match in matchesName)
tempStrResult = match.Value;
if (!string.IsNullOrEmpty(tempStrResult))
result.Lastname = tempStrResult.Split(" ")[0];
if (tempStrResult.Split(" ").Length > 1)
result.Firstname = tempStrResult.Split(" ")[1];
if (!string.IsNullOrEmpty(dbFullName) && string.IsNullOrEmpty(tempStrResult))
MatchCollection matchesFullName = rg.Matches(dbFullName);
if (matchesFullName.Count > 0)
foreach (Match match in matchesFullName)
tempStrResult = match.Value;
if (!string.IsNullOrEmpty(tempStrResult))
result.Lastname = tempStrResult.Split(" ")[0];
if (tempStrResult.Split(" ").Length > 1)
result.Firstname = tempStrResult.Split(" ")[1];