Regex for matching a music Chord

前端 未结 4 1722
野的像风
野的像风 2020-12-17 21:22

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)

4条回答
  •  Happy的楠姐
    2020-12-17 22:21

    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)?)?

提交回复
热议问题