# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\n(\d{4}).*(?:\n\(.*)*\n\[(?: *|\d+-\d+-\d+)]"
test_str = ("\n"
"0047 Heptasilver nitrate octaoxide\n"
"[12258-22-9] Ag NO\n"
"7 11\n"
"(Ag O ) .AgNO\n"
"3 4 2 3\n"
"Alone, or Sulfides, or Nonmetals\n"
"The crystalline product produced by electrolytic oxidation\n"
"of silver nitrate (and possibly as formulated) detonates\n"
"feebly at 110°C. Mixtures with phosphorus and sulfur\n"
"explode on impact, hydrogen sulfide ignites on contact,\n"
"and antimony trisulfide ignites when ground with the salt.\n"
"Mellor, 1941, Vol. 3, 483–485\n"
"See other SILVER COMPOUNDS\n"
"See related METAL NITRATES\n"
"0048 Aluminium\n"
"[7429-90-5] Al\n"
"Al\n"
"HCS 1980, 135 (powder)\n"
"Finely divided aluminium powder or dust forms highly\n"
"explosive dispersions in air [1], and all aspects of pre-\n"
"vention of aluminium dust explosions are covered in 2\n"
"US National Fire Codes [2]. The effects on the ignition\n"
"properties of impurities introduced by recycled metal used\n"
"to prepare dust were studied [3]. Pyrophoricity is elimi-\n"
"nated by surface coating aluminium powder with poly-\n"
"styrene [4]. Explosion hazards involved in arc and flame\n"
"spraying of the powder were analyzed and discussed [5],\n"
"and the effect of surface oxide layers on flammability\n"
"was studied [6]. The causes of a severe explosion in\n"
"1983 in a plant producing fine aluminium powder were\n"
"analyzed, and improvements in safety practices discussed\n"
"[7]. A number of fires and explosions involving aluminiumdust arising from grinding, polishing, and buffing opera-\n"
"tions were discussed, and precautions detailed [8] [12]\n"
"[13]. Atomized and flake aluminium powders attain\n"
"See other METALS\n"
"See other REDUCANTS\n"
"0049 Aluminium-cobalt alloy (Raney cobalt alloy)\n"
"[37271-59-3] 50:50; [12043-56-0] Al Co; Al—Co\n"
"5\n"
"[73730-53-7] Al Co\n"
"2\n"
"Al Co\n"
"The finely powdered Raney cobalt alloy is a significant\n"
"dust explosion hazard.\n"
"See DUST EXPLOSION INCIDENTS (reference 22)\n"
"0050 Aluminium–copper–zinc alloy\n"
"(Devarda’s alloy)\n"
"[8049-11-4] Al—Cu—Zn\n"
"Al Cu Zn\n"
"Silver nitrate: Ammonia, etc.\n"
"See DEVARDA’S ALLOY\n"
"See other ALLOYS0051 Aluminium amalgam (Aluminium–\n"
"mercury alloy)\n"
"[12003-69-9] (1:1) Al—Hg\n"
"Al Hg\n"
"The amalgamated aluminium wool remaining from prepa-\n"
"ration of triphenylaluminium will rapidly oxidize and\n"
"become hot upon exposure to air. Careful disposal is nec-\n"
"essary [1]. Amalgamated aluminium foil may be pyro-\n"
"phoric and should be kept moist and used immediately [2].\n"
"1. Neely, T. A. et al., Org. Synth., 1965, 45, 109\n"
"2. Calder, A. et al., Org. Synth., 1975, 52, 78\n"
"See other ALLOYS")
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