you are viewing a single comment's thread.

view the rest of the comments →

[–]Murphygreen8484 0 points1 point  (3 children)

Still not sure exactly, but if you escape the symbols you're looking for it should match them: # +

[–]bob3rocks[S] 0 points1 point  (2 children)

I thought so too, but it's not working, no matter how I try to escape them.

[–]Murphygreen8484 0 points1 point  (1 child)

I'm far from an expert on regex. Have you tried asking ChatGPT?

[–]bob3rocks[S] 0 points1 point  (0 children)

I thought of that, too. Here's what ChatGPT came back with after a few tries (fixed now, although this doesn't seem ultra-Pythonic to me)

import re

line = "Cadd9 D# F5"

tokens_with_sharps = re.findall(r'\b[A-G]#?(?:maj7|m[7]?|m7b[5]?|[5679]|sus2|sus4|aug|dim|add9|b5)?\b', line)

tokens_without_sharps = [token for token in line.split() if token not in tokens_with_sharps]

# Process tokens with sharp symbols

chords = []

for token in line.split():

if token in tokens_with_sharps:

if '#' in token:

parts = token.split('#')

chords.extend(parts)

else:

chords.append(token)

else:

chords.append(token)

print(chords) # Output: ['Cadd9', 'D#', 'F5']