Grep help for musicxml

25 views
Skip to first unread message

Marcel Lamers

unread,
Dec 5, 2022, 11:13:42 AM12/5/22
to BBEdit Talk
Hi,

I have a musicxml file that is not properly imported into my music notation app and I have to change the syntax to do a better import.
I gave it a go with grep but can't seem to get the proper pattern to make the change.

In short, I want to remove the dynamics tags plus content from a note tag and place it before the note tag with new closing tags.
Difficulty is the occurrence of several <note> tags and the greediness of the patterns I tested.
I also gave the must not occur <note>(?!</note>) a go but to no avail. Any help in the matter would be greatly appreciated

Here is an example:

before

<!--//////////////////// Part 22 | Measure 39 ////////////////////-->
<measure number="39">
<attributes>
<divisions>48</divisions>
</attributes>
<note>
<pitch>
<step>D</step>
<octave>6</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<note>
<pitch>
<step>G</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<note>
<pitch>
<step>D</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<direction>
<direction-type>
<wedge type="stop"/>
</direction-type>
</direction>
<note>
<pitch>
<step>G</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
<notations>
<slur type="stop" number="1"/>
<dynamics>
<f/>
</dynamics>

</notations>
</note>
</measure>
<!--//////////////////// Part 22 | Measure 40 ////////////////////-->

after

<!--//////////////////// Part 22 | Measure 39 ////////////////////-->
<measure number="39">
<attributes>
<divisions>48</divisions>
</attributes>
<note>
<pitch>
<step>D</step>
<octave>6</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<note>
<pitch>
<step>G</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<note>
<pitch>
<step>D</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
</note>
<direction>
<direction-type>
<wedge type="stop"/>
</direction-type>
</direction>
<direction>
<direction-type>
<dynamics>
<f/>
</dynamics>
</direction-type>
</direction>
<note>
<pitch>
<step>G</step>
<octave>5</octave>
</pitch>
<duration>48</duration>
<voice>1</voice>
<type>quarter</type>
<staff>1</staff>
<notations>
<slur type="stop" number="1"/>
</notations>
</note>
</measure>
<!--//////////////////// Part 22 | Measure 40 ////////////////////-->

Marcel Lamers

unread,
Dec 5, 2022, 2:27:49 PM12/5/22
to BBEdit Talk
It gets much simpler if I add e.g. a ! before every </note> first.
I can then search for
(<note>)([^!]+)(<dynamics>)([^!]+)(</dynamics>)

jj

unread,
Dec 5, 2022, 3:45:30 PM12/5/22
to BBEdit Talk
Hi Marcel,

You could try this.

Find:

    (?s)(<note(?:.(?!</note>))+)(<dynamics.+?</dynamics>)\n((?:.(?!<note))+</note>)

Replace:

    <direction>\n<direction-type>\n\2\n</direction-type>\n</direction>\n\1\3

HTH

Jean Jourdain

Marcel Lamers

unread,
Dec 6, 2022, 8:43:13 AM12/6/22
to BBEdit Talk
Thx JJ, that works great!
Reply all
Reply to author
Forward
0 new messages