Search code examples
regexmusic-notation

Regex for matching a music Chord


I am trying to write a function to parse the string representation of a musical chord.

Example: C major chord -> Cmaj (this is what I want to parse)

Just to make it clear, a chord is made of three different parts:

  • the note (C, D, E, F, G, A)
  • the accidentals for that note (#, ##, b, bb)
  • the chord name

For those, music savvy, I am not considering slash chords (on purpose).

The below function is almost working. However it still doesn't work for the following case:

  • "C#maj" # matches and should
  • "C#maj7" # matches and should
  • "C#maj2" # mathches and shouldn't

I suppose that if I could make the chords regex part forced to be at the end of the regex, did the trick. I have tried using the $ both before and after this String but it didn't work.

Any idea? Thanks.

public static void regex(String chord) {                
    String notes = "^[CDEFGAB]";
    String accidentals = "[#|##|b|bb]";
    String chords = "[maj7|maj|min7|min|sus2]";
    String regex = notes + accidentals + chords; 
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(chord);
    System.out.println("regex is " + regex);
    if (matcher.find()) {
        int i = matcher.start();
        int j = matcher.end();
        System.out.println("i:" + i + " j:" + j);           
    }
    else {
        System.out.println("no match!");
    }
}

Solution

  • Change [ and ] to ( and ) in the following lines:

    String accidentals = "(#|##|b|bb)";
    String chords = "(maj7|maj|min7|min|sus2)";
    

    Otherwise you're just making character classes, so [maj7|maj|min7|min|sus2] simply matches on the letter m.

    I'm guessing you also want to add an ending anchor $? I see you had problems with that before, but that's probably because of the aforementioned issue.


    Also, might you want (#|##|b|bb) to be optional (i.e., with ?: (#|##|b|bb)?)?