use strict;
my $str = '{"message":"{\\"_\\":\\"user\\",\\"pFlags\\":{\\"contact\\":true},\\"user_flags\\":2143,\\"id\\":702212125,\\"access_hash\\":\\"914250561826\\",\\"first_name\\":\\"david\\",\\"last_name\\":\\"jones\\",\\"username\\":\\"david_d192\\",\\"phone\\":\\"051863329875\\",\\"status\\":{\\"_\\":\\"userStatusRecently\\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"username":"david_d192","type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
{"message":"{\\"_\\":\\"user\\",\\"pFlags\\":{\\"contact\\":true},\\"user_flags\\":2143,\\"id\\":702212125,\\"access_hash\\":\\"914250561826\\",\\"first_name\\":\\"david\\",\\"last_name\\":\\"jones\\",\\"username\\":\\"david_d192\\",\\"phone\\":\\"051863329875\\",\\"status\\":{\\"_\\":\\"userStatusRecently\\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"username":"david_d192","type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
{"message":"{\\"_\\":\\"user\\",\\"pFlags\\":{\\"contact\\":true},\\"user_flags\\":2143,\\"id\\":702212125,\\"access_hash\\":\\"914250561826\\",\\"first_name\\":\\"david\\",\\"last_name\\":\\"jones\\",\\"phone\\":\\"051863329875\\",\\"status\\":{\\"_\\":\\"userStatusRecently\\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
#Target: id, first_name , last_name , phone , username(if exist)';
my $regex = qr/(?<=last_name\\\":\\\")(\w)+(?=\\)/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