import re
regex = re.compile(r"(?:BUY|SELL|LIMIT|STOP)(?:.*?)([A-Z]{6,})|([A-Z(GOLD)]{6,})(?:.*?)[\s*@#](?:BUY|SELL)|(GOLD)", flags=re.MULTILINE)
test_str = ("XAUUSD BUY LIMIT@ 1803\n"
"XAUUSD SELL @1800\n"
"GOLD SELL\n"
"XAUUSD BUY@ 1811.3-1810\n"
"XAUUSD BUY NOW 1811.2\n"
"XAUUSD BUY 1811.3\n"
"XAUUSD BUY @ 1811.3\n"
"BUY LIMIT @XAUUSD@ 1811.\n"
"XAUUSD@BUY 1811.\n"
"XAUUSD@ BUY 1811.\n"
"XAUUSD @BUY 1811.\n"
"BUY LIMIT XAUUSD @1811.3\n"
"SELL LIMIT XAUUSD\n"
"U//scvxcxSELL XAUUSD\n"
"UxxczcSELL GOLD\n"
"xauusd buy@321321.23")
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