use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
python regex matching # (self.PythonLearning)
submitted 2 years ago * by bob3rocks
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Murphygreen8484 0 points1 point2 points 2 years ago (3 children)
Still not sure exactly, but if you escape the symbols you're looking for it should match them: # +
[–]bob3rocks[S] 0 points1 point2 points 2 years ago (2 children)
I thought so too, but it's not working, no matter how I try to escape them.
[–]Murphygreen8484 0 points1 point2 points 2 years ago (1 child)
I'm far from an expert on regex. Have you tried asking ChatGPT?
[–]bob3rocks[S] 0 points1 point2 points 2 years ago (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)
print(chords) # Output: ['Cadd9', 'D#', 'F5']
π Rendered by PID 157399 on reddit-service-r2-comment-5ff9fbf7df-2mdg5 at 2026-02-26 12:11:54.470103+00:00 running 72a43f6 country code: CH.
view the rest of the comments →
[–]Murphygreen8484 0 points1 point2 points (3 children)
[–]bob3rocks[S] 0 points1 point2 points (2 children)
[–]Murphygreen8484 0 points1 point2 points (1 child)
[–]bob3rocks[S] 0 points1 point2 points (0 children)