# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?ms)pid.*?req"
test_str = ("Format which I need:\n\n"
"2019-11-10 15:22:32.662 +0300 (Default,mydzit.gov.sa\\kalsolai,4C8EC4A70F3149988B93370B16CADBEE-0:0,XcgBCNSpBpjM51ezrQYXrQAAAgE) catalina-exec-150 : INFO wgsessionId=rd9xUhIUR92tuPbGQkZrNQ com.tableausoftware.domain.session.SessionService - Non-guest user session found.\n\n"
"AND:\n\n"
"2019-11-10 15:22:40.816 +0300 (Default,mydzit.gov.sa\\kalsolai,F853E8F10C2742CBB071A090C3047519-0:0,XcgBENSpBpjM51ezrQYXvwAAAZE) catalina-exec-151 : INFO wgsessionId=rd9xUhIUR92tuPbGQkZrNQ com.tableausoftware.model.vizql.util.WithSessionAspect - Command not allowed on a shared session F853E8F10C2742CBB071A090C3047519. Cloning to a private session and retrying.\n\n"
"Format which no need:\n\n"
"first form:\n"
"{ [-]\n"
"k: msg\n"
"pid: 37552\n"
"req: -\n"
"sess: -\n"
"sev: info\n"
"site: -\n"
"tid: 4958\n"
"ts: 2019-11-06T17:06:06.305\n"
"user: -\n"
"v: Resource Manager: Memory info: 73,793,536 bytes (current process);52,410,015,744 bytes (Tableau total); 49,979,158,528 bytes (total of all processes); 30 (info count)\n"
"}\n\n"
"Second Form:\n"
"{\"ts\":\"2019-11-06T17:06:06.305\",\"pid\":37552,\"tid\":\"4958\",\"sev\":\"info\",\"req\":\"-\",\"sess\":\"-\",\"site\":\"-\",\"user\":\"-\",\"k\":\"msg\",\"v\":\"Resource Manager: Memory info: 73,793,536 bytes (current process);52,410,015,744 bytes (Tableau total); 49,979,158,528 bytes (total of all processes); 30 (info count)\"}")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# 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