[PD] Parsing Pd patches in Javascript, Python, Java

Chris McCormick chris at mccormick.cx
Mon Nov 29 05:46:57 CET 2010


Hi,

Here is how you can parse Pd patches into rows of atoms in three languages using regular expressions:

/*** Javascript ***/
        
	var lines_re = new RegExp("(#((.|\r|\n)*?)[^\\\\])\r{0,1}\n{0,1};\r{0,1}\n", "gi");
        for (pdline = lines_re.exec(patchtext)) {
                var atoms = pdline[1].split(/ |\r\n?|\n/);
        }

### Python ###

	lines_re = re.compile("(#(.*?)[^\\\])\r{0,1}\n{0,1};\r{0,1}\n", re.MULTILINE | re.DOTALL)
	split_re = re.compile(" |\r\n?|\n", re.MULTILINE)
	for found in lines_re.finditer(patch):
		line = found.group(1)
		atoms = split_re.split(line)

/*** Java ***/

	private static final String line_re = "(#((.|\r|\n)*?)[^\\\\])\r{0,1}\n{0,1};\r{0,1}\n";
	private static final String token_re = " |\r\n?|\n";
	
	Pattern pattern = Pattern.compile(line_re, Pattern.MULTILINE);
	Pattern token_pattern = Pattern.compile(token_re, Pattern.MULTILINE);
	Matcher matcher = pattern.matcher(patchtext);
	ArrayList<String[]> atomlines = new ArrayList<String[]>();
	while (matcher.find()) {
		String[] s = token_pattern.split(matcher.group(1));
		atomlines.add(token_pattern.split(matcher.group(1)));
	}

Also here is a regular expression for matching dollar args: 

	/(?:\\{0,1}\$)(\d+)/g;

Hopefully this is useful to someone else.

Cheers,

Chris.

-------------------
http://mccormick.cx



More information about the Pd-list mailing list