import re
regex = re.compile(r"(?<=last_name\\\":\\\")(\w)+(?=\\)", flags=re.MULTILINE)
test_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\"}\n"
"{\"index\": {\"_type\": \"_doc\", \"_id\": \"36GG54F\"}}\n\n"
"{\"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\"}\n"
"{\"index\": {\"_type\": \"_doc\", \"_id\": \"36GG54F\"}}\n\n"
"{\"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\"}\n"
"{\"index\": {\"_type\": \"_doc\", \"_id\": \"36GG54F\"}}\n\n"
"#Target: id, first_name , last_name , phone , username(if exist)")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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 Python, please visit: https://docs.python.org/3/library/re.html