use strict;
my $str = '29/03/14 15:48:05: John Smith changed the subject to “Testâ€
29/03/14 16:10:39: John Smith joined
29/03/14 16:10:40: Person:2 joined
29/03/14 16:10:40: John Smith: Hello!
29/03/14 16:11:40: Person:2: some random words,
29/03/14 16:12:40: Person3 joined
29/03/14 16:13:40: John: Smith: Hello!Test message with newline
Another line of the same message
Another line.
29/03/14 16:14:43: Person:2: Test message using as last word joined
29/03/14 16:15:57: Person3 left
29/03/14 16:17:16: Person3 joined
29/03/14 16:18:21: Person:2 changed the group icon
29/03/14 16:19:16: Person3 was removed
29/03/14 16:20:43: Person:2: Test message using as last word left';
my $regex = qr/(?P<datetime>\d{2}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}): (?P<name>\w+(?::\s*\w+)*|[\w\s]+?)(?:\s+(?P<action>joined|left|was removed|changed the (?:subject to “\w+â€|group icon))|:\s(?P<message>(?:.+|\n(?!\n))+))/p;
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