use strict;
my $str = '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
';
my $regex = qr/^[^\S\n]*(\w+(?: +\w+)*?)(?=:| joined the lobby$)/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html