private InstrumentType matchInstrument(String instrumentData) throws ParseException { InstrumentType matchedType; StringBuilder instrumentPatternsBuilder = new StringBuilder(); for (InstrumentType instrumentType : InstrumentType.values()) { instrumentPatternsBuilder.append( String.format("|(?<%s>%s)", instrumentType.name(), instrumentType.pattern)); } Pattern instrumentPattern = Pattern.compile(instrumentPatternsBuilder.substring(1), Pattern.CASE_INSENSITIVE); Matcher matcher = instrumentPattern.matcher(instrumentData); if (matcher.find()) { if (matcher.group(InstrumentType.HIGHHAT.name()) != null) { matchedType = InstrumentType.HIGHHAT; } else if (matcher.group(InstrumentType.BASSDRUM.name()) != null) { matchedType = InstrumentType.BASSDRUM; } else if (matcher.group(InstrumentType.CRASHCYMBAL.name()) != null) { matchedType = InstrumentType.CRASHCYMBAL; } else if (matcher.group(InstrumentType.RIDECYMBAL.name()) != null) { matchedType = InstrumentType.RIDECYMBAL; } else if (matcher.group(InstrumentType.SNAREDRUM.name()) != null) { matchedType = InstrumentType.SNAREDRUM; } else if (matcher.group(InstrumentType.HIGHTOM.name()) != null) { matchedType = InstrumentType.HIGHTOM; } else if (matcher.group(InstrumentType.LOWTOM.name()) != null) { matchedType = InstrumentType.LOWTOM; } else if (matcher.group(InstrumentType.FLOORTOM.name()) != null) { matchedType = InstrumentType.FLOORTOM; } else { throw new ParseException(instrumentData, 0); } } else { throw new ParseException(instrumentData, 0); } return matchedType; }