I would like to understand this specific regExp formatting
Hello guys, I have copied this small mechanism from the forum, which gets the track id of the track where the device is loaded.
Since I copied it I don't really understand the regExp format so if anyone could break it down for me it would be lovely, also because i know regExp and sprintf are relly really powerfull tools and would like to get more into them!
Thanks in Advance.
Patch v v v v v v
{
"boxes" : [ {
"box" : {
"maxclass" : "newobj",
"text" : "live.thisdevice",
"numinlets" : 1,
"color" : [ 0.72156862745098, 0.419607843137255, 0.113725490196078, 1.0 ],
"numoutlets" : 3,
"outlettype" : [ "bang", "int", "int" ],
"id" : "obj-140",
"patching_rect" : [ 323.357662498950958, 754.347832381725311, 77.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "path live_set tracks 4",
"presentation_linecount" : 3,
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-128",
"patching_rect" : [ 126.690875589847565, 922.857164859771729, 215.666786909103394, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "regexp (path\\\\slive_set\\\\s(tracks\\\\s\\\\d+|master_track|return_tracks\\\\s\\\\d+)).* @substitute %1",
"numinlets" : 1,
"numoutlets" : 5,
"outlettype" : [ "", "", "", "", "" ],
"id" : "obj-130",
"patching_rect" : [ 323.188408493995667, 878.260876893997192, 493.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "t getpath l",
"numinlets" : 1,
"fontsize" : 10.0,
"numoutlets" : 2,
"fontname" : "Arial",
"outlettype" : [ "", "" ],
"id" : "obj-131",
"patching_rect" : [ 323.357662498950958, 827.857162594795227, 64.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "live.object",
"numinlets" : 2,
"fontsize" : 10.0,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "" ],
"id" : "obj-133",
"patching_rect" : [ 323.357662498950958, 851.449282467365265, 64.0, 20.0 ],
"saved_object_attributes" : {
"_persistence" : 0
}
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "path this_device",
"numinlets" : 2,
"fontsize" : 10.0,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "" ],
"id" : "obj-134",
"patching_rect" : [ 323.357662498950958, 779.710151433944702, 90.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "live.path",
"numinlets" : 1,
"fontsize" : 10.0,
"numoutlets" : 3,
"fontname" : "Arial",
"outlettype" : [ "", "", "" ],
"id" : "obj-135",
"patching_rect" : [ 323.357662498950958, 805.71430492401123, 51.0, 20.0 ]
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-140", 0 ],
"destination" : [ "obj-134", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-133", 0 ],
"destination" : [ "obj-130", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-130", 0 ],
"destination" : [ "obj-128", 1 ]
}
}
, {
"patchline" : {
"source" : [ "obj-135", 0 ],
"destination" : [ "obj-131", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-134", 0 ],
"destination" : [ "obj-135", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-131", 0 ],
"destination" : [ "obj-133", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-131", 1 ],
"destination" : [ "obj-133", 1 ]
}
}
],
"appversion" : {
"major" : 8,
"minor" : 6,
"revision" : 1,
"architecture" : "x64",
"modernui" : 1
}
,
"classnamespace" : "box"
}
Two tips:
1) There is a much more efficient way to share patches than using this long-winded format. In your patch, select all the objects that you want to share and then choose Copy Compressed from the Edit menu to copy these objects to the clipboard in a compressed format. In your message here, when you paste the contents of the clipboard into your message, it will create a specially formatted object that others can download from. To download and analyse a posted patch, click on the blue icon and then use New From Clipboard in the File menu in Max.
2) Have you looked at the help files and reference pages available in Max? You can open these with a right-click on the object in Windows (probably Alt-Click or Cmd-click on Mac) or by selecting the object and selecting Help or Reference from the Help menu.
[sprintf] in Max is really an extension that links to the C++ library object. All the formatting options are therefore the same as you'll find in any C++ manual, of which there are many on the internet.
This is what your patch would look like if you copied and pasted it in the compressed format:
you can use this website to figure out regex: https://regex101.com/
keep in mind that max's regex object requires double backslashes \\ while regex101 requires only one \
regexp (path\\slive_set\\s(tracks\\s\\d+|master_track|return_tracks\\s\\d+)).* @substitute %1
that too long regexp matches set strings and outputs if something matched.
If one looks what it does, it only removes device & it's number
because output of path to this device will allways be path live_set bla bla ...
much more effective would instead be
regexp devices\\s\\d+ @substitute %0
which would strip device and pass the rest.
even better
we all know that path live_set stuff, so why not strip it too and leave only needed info
regexp (path\\slive_set|devices\\s\\d+) @substitute %0