import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\n(\\d{4}).*(?:\\n\\(.*)*\\n\\[(?: *|\\d+-\\d+-\\d+)]";
final String string = "\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";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html