Python: embeddable regex?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
davedrowsy
Posts: 26
Joined: Wed Jan 16, 2013 5:27 pm UTC
Location: Durham, NC

Python: embeddable regex?

Postby davedrowsy » Thu Jan 17, 2013 7:50 pm UTC

I'm fairly new to Python and regular expressions, and I'm trying to figure out how to solve this particular problem... I know there's a solution, either with or without using regular expressions...

Here's what I'm trying to do: I'm writing a music programming language parser that will parse a string of musical "note data" (for example, "c4 d8 e f g a b > c" -- it's a lot like MCK MML, if anyone's familiar with that), break it up into a list of single "events" which could be notes, chords, rests, etc., and then for each event, use regular expressions to be able to determine which "type" the event belongs to. For example, in the above string, the parser would determine c4=note, d8=note, e=note, (...), >=octave change, c = note. I've gotten the parser to correctly match notes, octave changes and rests, but I'm having difficulty implementing chords. The syntax for a chord is "note/note/note/note," where "note" is any string matching my regex definition of a note. A chord can contain 2 or more notes.

My regex definition of a note is going to be (and already is) somewhat complex, so I'm wondering if there is some way to embed my regex for a note into the regex for a chord? That way my regex to match a chord would be something like "^note(/note)+$" -- in other words, at least 2 strings that match the note regex, each separated by a / character. Is there a way to do this with regex in Python?

EDIT: On further thought, I could just paste the actual "note" regex into the "chord" regex, but it would still be more convenient if I could somehow embed it in there like a variable. That way any time I need to make changes to the syntax for a note, I can just edit the regex for notes, and the chord regex will still work the way it's supposed to.

User avatar
scarecrovv
It's pronounced 'double u'
Posts: 674
Joined: Wed Jul 30, 2008 4:09 pm UTC
Location: California

Re: Python: embeddable regex?

Postby scarecrovv » Thu Jan 17, 2013 8:25 pm UTC

Well in Python, the regexes you pass to the re library are strings, so you could do something like this:

Code: Select all

note = "[a-g][0-9]"                 # your note regex
noteRegex = re.compile(note)

chord = "(" + note + "/)+" + note   # your chord regex, assembled by concatenating strings, some of which are your note regex
chordRegex = re.compile(chord)

User avatar
davedrowsy
Posts: 26
Joined: Wed Jan 16, 2013 5:27 pm UTC
Location: Durham, NC

Re: Python: embeddable regex?

Postby davedrowsy » Thu Jan 17, 2013 8:42 pm UTC

Aha! That solves it nicely. Thanks! :D

User avatar
phlip
Restorer of Worlds
Posts: 7569
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Python: embeddable regex?

Postby phlip » Fri Jan 18, 2013 5:56 am UTC

Some random spitballing that might make things more readable if you have a lot of these, and they're all interrelated...

Code: Select all

regexes = {}
regexes['note'] = "[a-g][0-9]"
regexes['chord'] = "(?:%(note)s/)+%(note)s" % regexes
regexes['event'] = "(?:%(note)s|%(chord)s)" % regexes

regexes = {k:re.compile(v) for k,v in regexes.iteritems()}

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests