import re
regex = re.compile(r"((?:\\u[\da-fA-F]{4}){2})")
test_str = ("Hi, Jax\\ud83d\\ude1b\\ud83d\\ude44! can we go for a coffee?\n"
"Now, the emoji are in UTF16(I think). I need to extract the '\\ud83d\\ude1b\\ud83d\\ude44' and give a space between each pair, something like this.\n\n"
"Hi, Jax\\ud83d\\ude1b \\ud83d\\ude44! can we go for a coffee?\n"
"How can I achieve this in PHP?\n\n"
"More Examples as I would need:-\n\n"
"Hi, Jax\\ud83d\\ude1b \\ud83d\\ude44! can we go for\\ud83d\\ude1b \\ud83d\\ude44 a coffee?\n"
"So What needed to be done:-\n\n"
"User may or may not leave any space after any normal word and just type a emoji. I mean, Jax\\ud83d\\ude1b and Jax \\ud83d\\ude1b.")
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