# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"[^\d\.](((01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3}[\. ]?[\dX]{2}[\. \-]?[\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3}[\. ]?[\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\. ]?[\dX]{3})(?![\d\.X]))|([^\d\.](01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)(?![\d\.X]|( ?%)))"
test_str = (" HS codes: 271012 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations; 33.234-33.237 waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containinandandandg biodiesel and other than waste oils : Light oils and preparations), 271019 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the \n"
"preparations; waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containing biodiesel and other than waste oils : Other);\n"
"ICS codes: 03.120 (Quality), 13.020 (Environmental protection), 75.160 (Fuels);HS 1234 2134 5325 ösadlkjföasfas \n"
"HS: 8418 694X\n"
"HS: 8415 10XX\n"
"HS: 8418 694X\n"
"HS: 8415 10XX\n"
"HS 8418 101X, 8418 21XX\n"
"ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes);ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes); \n"
"ICS codes: 43.040.65 (Glazing and wiper systems)\n"
"HS codes:\n"
"39.26.90.90.90\n"
"70.07.11.00.00\n"
"70.07.21.00.00\n"
"234\n"
"87.08.29.50.00\n"
"ICS-Codes:\n"
"83.160 (Tyres)\n"
"(HS 4011).\n"
"Electrical and electronic equipment (EEE). More detail on product coverage can be found in Restriction of the Use of Certain Hazardous Substances in Electrical and Electro 23XX n ic Equipment Regulations 2012 (SI 2012/3032) (https://www.legislation.gov.uk/uksi/2012/3032/made). HS code 9018 for medical devices. Electronic Components in general (ICS 31.020); Electronic components in general (ICS 31.020)\n"
"Air emission sources; Quality (ICS code(s): 03.120); Environmental protection (ICS code(s): 13.020); Air quality (ICS code(s): 13.040); Test conditions and procedures in general (ICS code(s): 19.020)\n"
"Passenger car (including New Energy Vehicle) (HS code(s): 8703); (ICS code(s): 43.060; 43.080.01; 43.100; 75.160)\n"
"ICS codes: 29, 33, 97, 29 , 33, 97;\n\n"
"75.16X\n\n"
"13\n"
"13.123.34-34")
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