import re import time bad_expression = re.compile(' IN \((\d+,? ?)+\)') cases = { 'test_01': 'SQL_stuff IN (23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_02': 'SQL_stuff IN (23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_03': 'SQL_stuff IN (23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_04': 'SQL_stuff IN (23298, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_05': 'SQL_stuff IN (232981, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_06': 'SQL_stuff IN (2329812, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_07': 'SQL_stuff IN (23298123, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_08': 'SQL_stuff IN (232981234, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_09': 'SQL_stuff IN (2329812345, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_10': 'SQL_stuff IN (23298123456, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_11': 'SQL_stuff IN (12345, 23298, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_12': 'SQL_stuff IN (1234511111, 23298, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_13': 'SQL_stuff IN (1234511111, 2329811111, 23598, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', 'test_14': 'SQL_stuff IN (1234511111, 2329811111, 2359811111, 23298, 23598, 23607, NULL, 23599, 23583) ORDER BY "files_picture"."created" ASC', } for test_case, statement in cases.items(): print("Attempting " + test_case) start = time.time() bad_expression.sub("IN_GROUP", statement) delta = time.time() - start print("Done in {t}ms".format(t=round(delta * 1000, 2)))