using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^[^\S\n]*(\w+(?: +\w+)*?)(?=:| joined the lobby$)";
string input = @"Hey Guys,
I want to filter the usernames out of the following string from a game lobby:
Username123 joined the lobby
User name 123 joined the lobby
Otheruser 12 joined the lobby
Username123: Hello Guys!
o t h e ruser23 joined the lobby
Player1 3 joined the lobby
As you can see, the username can consist of characters, numbers and spaces. Before every Username a new line starts and the ""joined the lobby"" part is fixed. However between 2 users joining an already joined user can write a textmessage to the whole lobby.
How can I extract the 5 unique usernames?
I joined the lobby
";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx