import re
regex = re.compile(r"\[buyer_(?:email|mobile|fullname|address)\] =>\s*\K.*", flags=re.MULTILINE)
test_str = ("SimpleXMLElement Object\n"
"(\n"
"[error_code] => 00\n"
"[token] => 1182263526325de72aw828369e7aa\n"
"[description] => SimpleXMLElement Object\n"
" (\n"
" )\n\n"
"[transaction_status] => 00\n"
"[receiver_email] => myemail@gmail.com\n"
"[order_code] => 3212423\n"
"[total_amount] => 200\n"
"[payment_method] => SimpleXMLElement Object\n"
" (\n"
" )\n\n"
"[bank_code] => NLAZ\n"
"[payment_type] => 1\n"
"[order_description] => SimpleXMLElement Object\n"
" (\n"
" )\n\n"
"[tax_amount] => 0\n"
"[discount_amount] => 0\n"
"[fee_shipping] => 0\n"
"[return_url] => success.php\n"
"[cancel_url] => order.php\n"
"[buyer_fullname] => eric phuong\n"
"[buyer_email] => eric@domain.com\n"
"[buyer_mobile] => 012108609\n"
"[buyer_address] => abc\n"
"[affiliate_code] => SimpleXMLElement Object\n"
" (\n"
" )\n\n"
"[transaction_id] => 12345\n"
")")
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