# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"LIN[^L]*(?:L(?!IN)[^L]*)*"
test_str = "UNB+UNOA:2+094200005561400986LA:ZZ+MTEL+200406:1436+34906++++1'UNH+112490+DELFOR:D:96A:UN'BGM+241+2004060008796+9'DTM+137:202004061436:203'DTM+157:20200406:102'DTM+36:20200206:102'NAD+BY+FRSFA0222838V::92'NAD+SE+000563X::92'UNS+D'NAD+CN+VP1::92++TEST+SK TEST:204 TEST:TEST 22:TEST ST TEST+++37540+FRA'LIN+1+3+441344:IN'PIA+1+7PK1150:VN'IMD+++:::VO-VKMV 7PK1150 VP'LOC+11+999'LOC+159+999'RFF+ON:P092303'QTY+113:100.00:PC'SCC+1'DTM+2:20200116:102'RFF+AAJ:P092303:100'QTY+113:100.00:PC'SCC+1'DTM+2:20200206:102'RFF+AAJ:P092304:100'LIN+2+3+502107:IN'PIA+1+3PK670:VN'IMD+++:::VO-VKMV 3PK670 EDC'LOC+11+999'LOC+159+999'RFF+ON:P088273'QTY+113:300.00:PC'SCC+1'DTM+2:20190503:102'RFF+AAJ:P088273:100'LIN+3+3+502109:IN'PIA+1+6PK970:VN'IMD+++:::VO-VKMV 6PK970 EDC'LOC+11+999'LOC+159+999'RFF+ON:P084470'QTY+113:200.00:PC'SCC+1'DTM+2:20190422:102'RFF+AAJ:P084470:100'LIN+4+3+6DK1215:IN'PIA+1+AVRRV50D1-VKMV 6DK1215:VN'IMD+++:::6DK1215'LOC+11+999'LOC+159+999'RFF+ON:P046369'QTY+48:533.00:PC'RFF+AAK:32299'DTM+171:20181109:102'QTY+113:533.00:PC'SCC+1'DTM+2:20190419:102'RFF+AAJ:P046369:100'LIN+5+3+6DK1320:IN'PIA+1+AVRRV50D1-VKMV 6DK1320?+282:VN'IMD+++:::6DK1320'LOC+11+999'LOC+159+999'RFF+ON:P061903'QTY+48:115.00:PC'RFF+AAK:43146'DTM+171:20181003:102'QTY+113:104.00:PC'SCC+1'DTM+2:20181005:102'RFF+AAJ:P061903:100'QTY+113:104.00:PC'SCC+1'DTM+2:20181102:102'RFF+AAJ:P062034:100'UNS+S'UNT+75+112490'UNZ+1+34906'"
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