While the @Maniero solution largely solves the issue, the version is left with regular expressions to solve the problem:
private static Regex regex = new Regex("^.*\\((?<login>.*)\\)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
public static void Main()
{
WriteLine(GetLogin("ZACKSON MOREIRA MORGAN (zackson.morgan)"));
WriteLine(GetLogin("ZACKSON MOREIRA MORGAN (zackson.morgan"));
WriteLine(GetLogin("ZACKSON MOREIRA MORGAN )"));
WriteLine(GetLogin("ZACKSON MOREIRA MORGAN ("));
WriteLine(GetLogin("ZACKSON MOREIRA MORGAN"));
}
private static string GetLogin(string input)
{
var res = regex.Match(input);
if(res.Success)
return res.Groups["login"].Value;
else
return "No matches found";
}
In this case, the syntax (?<input>.*)
captures all characters between parentheses (defined by \\(
and \\)
).
See working on dotNetFiddle.
Detail: all strings in the list will always follow this format.
– Leomar de Souza