I am trying to add square brackets around chords in standard text documents formatted for guitar / text to make them more compatible with the OnSong app. I have rules, but I don’t understand how to match all possible combinations. Rules:
- Chords start from the same capital AG
- if the capital of AG is followed by a space, line break, #, b, m, sus, aug, dim, maj, min or / I would like to read it until the next space or line break (due to standard guitar formatting, there may be such a chord, like F # min / E, and instead of worrying about it, I just want to keep reading until the place).
- The regular expression must NOT match if the capital of AG is followed by another letter that is not listed (for example, the name "Ed" must not match)
- bonus points if you can understand how to make the “small world” NOT suitable because of the word “FOLLOW” that “A” is not a valid chord.
- super bonus points, if the substitution can remove the space before (when the line does not start), and after new brackets (to keep the alignment in place) - I was told that this will not work on close chords ... this is quite acceptable.
A few notes: this is for a helper script ... perfection is not required. I'm doing it manually right now, so random misses are all right. I am not trying to disassemble the details of chords, just to wrap them in []. While the standard layout is 1 row of chords, 1 row of lyrics, this cannot be taken into account, so I know that some scenarios will sometimes fail.
Testing source (chords are random for testing purposes, in case some musicians are going to listen to terrible music):
Db Dsus4/F
Should turn into:
[Db] [Dsus4/F
My first attempt closed me:
([AG]((?!\s).)*)
but it raised words that began with these letters. Now I went around in circles and only got to:
\b([CDEFGAB](#|##|b|bb|sus|maj|min|aug)?\b)
When I tried to use [^\s+] , I get mixed results that take away more of what I want, but also what I need. I seem to be just above my head. Any help would be greatly appreciated, and any explanation of how it works would be even better. Although I would like to find a solution, I would also really like to explain why it works ...
source share