# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
# brief
^
(?:\s*(\d+:)\s*)?
([VDIWEF])/(.*?)
\(\s*(\d+)\):\s+
(.*)
$
"""
test_str = (" 1: I/Vold ( 2311): Vold 2.1 (the revenge) firing up\n"
"12:D/Vold ( 2311): Volume loop state changing -1 (Initializing) -> 0 (No-Media)\n"
"W/Vold ( 2311): addMdmDev don't find a match usb device(1d6b_2) in isKnownMdmDev\n"
"W/Vold ( 2311): addMdmDev don't find a match usb device(1d57_ad03) in isKnownMdmDev\n"
"I/SystemServer( 3424): Connectivity Service\n"
"D/ConnectivityService( 3424): ConnectivityService starting up\n"
"V/ConnectivityService( 3424): mNetworkPreference9\n"
"D/ConnectivityService( 3424): *******netType=wifi,1,1,1,-1,true\n"
"I/ConnectivityService( 3424): NetworkAttributes naString: wifi,1,1,1,-1,true type: 1\n"
"D/ConnectivityService( 3424): *******netType=mobile,0,0,0,-1,true\n"
"I/ConnectivityService( 3424): NetworkAttributes naString: mobile,0,0,0,-1,true type: 0\n"
"D/ConnectivityService( 3424): *******netType=mobile_bluetooth,7,7,1,-1,true\n"
"I/ConnectivityService( 3424): NetworkAttributes naString: mobile_bluetooth,7,7,1,-1,true type: 7\n"
"D/ConnectivityService( 3424): *******netType=wifi_p2p,13,1,0,-1,true\n"
"I/ConnectivityService( 3424): NetworkAttributes naString: wifi_p2p,13,1,0,-1,true type: 13\n"
"D/ConnectivityService( 3424): *******netType=ethernet,9,9,1,-1,true\n"
"I/ConnectivityService( 3424): NetworkAttributes naString: ethernet,9,9,1,-1,true type: 9\n"
"E/ConnectivityService( 3424): Ignoring protectedNetwork 10\n"
"E/ConnectivityService( 3424): Ignoring protectedNetwork 11\n"
"E/ConnectivityService( 3424): Ignoring protectedNetwork 12")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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