Regex for standard guitar lyric/chord bracketing
This passes using your sample input and achieves all your "super bonus points" requirements:
String output = input.replaceAll("(?m)(^| )([A-G](##?|bb?)?((sus|maj|min|aug|dim)\\d?)?(/[A-G](##?|bb?)?)?)( (?!\\w)|$)", "[$2]");
This code turns this (as a single String with embedded line fees):
Db Dsus4/F# A Cbmin/C
A man can't be asked for that much to do
D/F# G A D#/E
And I can't sweep you off of your feet
Into this:
[Db] [Dsus4/F#] [A] [Cbmin/C]
A man can't be asked for that much to do
[D/F#] [G] [A] [D#/E]
And I can't sweep you off of your feet
I have some working regex for the case you provided, but not sure how it will work for others. The problem is that a line can start with A
, or it can be in the song line. I tried to work around it using the negative lookahead checking if the chord is followed by a space and an alphanumeric. If there is a space and an alphanumeric, we do not match this chord. Since the chords can repeat after /
, I am doubling the pattern.
\b([CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug)*[\d\/]*(?:[CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug)*[\d\/]*)*)(?=\s|$)(?! \w)
Have a look at the demo.
I have improved a little bit the answer from previous answer to help in my case. Now It was ignoring some "chords likely" when it is in the beginning of the verse (like A, E). This what I came out:
(\(*[CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug|m|M|°|[0-9])*[\(]?[\d\/]*[\)]?(?:[CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug|m|M|°|[0-9])*[\d\/]*)*\)*)(?=[\s|$])(?! [a-z])