# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(^\w{3} \d{1,2} [0-9:]{8}) (\b\w+[.]\w*\b) +(\b\w*\b): (\b\w+\b) ([\w' ]*).*\(([\w.]*)\)$"
test_str = ("Jan 31 00:09:39 ubuntu.local ticky: INFO Created ticket [#4217] (mdouglas)\n"
"Jan 31 00:16:25 ubuntu.local ticky: INFO Closed ticket [#1754] (noel)\n"
"Jan 31 00:21:30 ubuntu.local ticky: ERROR The ticket was modified while updating (breee)\n"
"Jan 31 00:44:34 ubuntu.local ticky: ERROR Permission denied while closing ticket (ac)\n"
"Jan 31 01:00:50 ubuntu.local ticky: INFO Commented on ticket [#4709] (blossom)\n"
"Jan 31 01:29:16 ubuntu.local ticky: INFO Commented on ticket [#6518] (rr.robinson)\n"
"Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)\n"
"Jan 31 01:43:10 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens)\n"
"Jan 31 01:49:29 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mdouglas)\n"
"Jan 31 02:30:04 ubuntu.local ticky: ERROR Timeout while retrieving information (oren)\n"
"Jan 31 02:55:31 ubuntu.local ticky: ERROR Ticket doesn't exist (xlg)\n"
"Jan 31 03:05:35 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n"
"Jan 31 03:08:55 ubuntu.local ticky: ERROR Ticket doesn't exist (blossom)\n"
"Jan 31 03:39:27 ubuntu.local ticky: ERROR The ticket was modified while updating (bpacheco)\n"
"Jan 31 03:47:24 ubuntu.local ticky: ERROR Ticket doesn't exist (enim.non)\n"
"Jan 31 04:30:04 ubuntu.local ticky: ERROR Permission denied while closing ticket (rr.robinson)\n"
"Jan 31 04:31:49 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n"
"Jan 31 04:32:49 ubuntu.local ticky: ERROR Timeout while retrieving information (mcintosh)\n"
"Jan 31 04:44:23 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n"
"Jan 31 04:44:46 ubuntu.local ticky: ERROR Connection to DB failed (jackowens)\n"
"Jan 31 04:49:28 ubuntu.local ticky: ERROR Permission denied while closing ticket (flavia)\n"
"Jan 31 05:12:39 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n"
"Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri)\n"
"Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee)\n"
"Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy)\n"
"Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel)\n"
"Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia)\n"
"Jan 31 05:57:46 ubuntu.local ticky: INFO Commented on ticket [#2253] (nonummy)\n"
"Jan 31 06:12:02 ubuntu.local ticky: ERROR Connection to DB failed (oren)")
subst = " \\1 \\2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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