I'm writing a text to cdr (chordpro) converter and I'm having trouble detecting chord lines on the form:
Cmaj7 F#m C7
Xxx xxxxxx xxx xxxxx xx x xxxxxxxxxxx xxx
This is my python code:
def getChordMatches(line):
import re
notes = "[CDEFGAB]";
accidentals = "(#|##|b|bb)?";
chords = "(maj|min|m|sus|aug|dim)?";
additions = "[0-9]?"
return re.findall(notes + accidentals + chords + additions, line)
I want it to return a list ["Cmaj7", "F#m", "C7"]. The above code doesn't work, I've struggled with the documentation, but I'm not getting anywhere.
Why doesn't it work to just chain the classes and groups together?
edit
Thanks, I ended up with the following which covers most (it won't match E#m11 for instance) of my needs.
def getChordMatches(line):
import re
notes = "[ABCDEFG]";
accidentals = "(?:#|##|b|bb)?";
chords = "(?:maj|min|m|sus|aug|dim)?"
additions = "[0-9]?"
chordFormPattern = notes + accidentals + chords + additions
fullPattern = chordFormPattern + "(?:/%s)?\s" % (notes + accidentals)
matches = [x.replace(' ', '').replace('\n', '') for x in re.findall(fullPattern, line)]
positions = [x.start() for x in re.finditer(fullPattern, line)]
return matches, positions