I am trying to add square brackets around chords in standard text documents formatted for guitar/lyrics to make them more compatible with the OnSong app. I have the rules but don't understand how to match for all the possible combinations. The rules are:
- Chords will begin with a single capital A-G
- if the capital A-G is followed by a space, line break, #, b, m, sus, aug, dim, maj, min, or / I'd like to read until the next whitespace or line break (because of standard guitar formatting, a chord like F#min/E is possible, and rather than bother splitting it all out, I just want to keep reading until space)
- The regex should NOT match if the capital A-G is followed by another letter not in the list above (for example the name "Ed" should not match)
- bonus points if you can figure out how to make "A small world" NOT match due to the word FOLLOWING the "A" not being a valid chord.
- super bonus points if the replacement can remove a space before (when not starting a line) and after the new brackets (to keep alignment in place) - it was pointed out to me that this will fail on close chords... this is completely acceptable.
A couple notes: This is for a helper script... perfection is not needed. I do this by hand right now, so the occasional miss is okay. I'm not trying to parse out the details of the chords, just to wrap them in []. While the standard layout is 1 row of chords, 1 row of lyrics, this can't be counted on, so I'm aware some scenarios will fail occasionally.
Test source (chords are random for testing purposes, in case any musicians were going to chime in on the terrible music):
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
Should turn into:
[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
My first attempt got me close with:
([A-G]((?!\s).)*)
but that picked up words that began with those letters as well. I have gone around in circles now and only gotten as far as:
\b([CDEFGAB](#|##|b|bb|sus|maj|min|aug)?\b)
When I've tried to use [^\s+]
I get mixed results that pick up more of what I want but also ditch things I need. I think I'm just over my head. Any help would be GREATLY appreciated and any explanation of how it works would be even better. While I'd like a solution, I'd also really love to explain why it works...
A# Eb // Oh man!
will become[A#][Eb] // Oh man!
? Due to the closeness of these chords, we loose the alignment (adding brackets). – Korikorie