interesting regexp problem

snerk4000's icon

currently working on a script to strip characters out of titles coming from live. using:

regexp [\W] @substitute _

works fine, EXCEPT when the title of the clip is one word with no spaces. then, nothing gets output at all.

is there a better way using regex to remove any non-alphanumeric characters coming down the pipe and replace them all with underscores? (and retain strings that have no spaces...) i can't figure out how to "escape" a space in the regexp without it throwing quotes in and making it not work.

any help is appreciated!

willyc's icon

I've run into this problem before, and used the following wrapper around my regexp objects to identify the case where no substitutions are made. It feels like tha patching is doing more work than I would ideally like, but does work - the print output should verify you are getting the expected number output

Alternatively, you could spin up a js object and do the regex work in javascript, sith something along the lines of:

function strip(input)
{
outlet(0, input.replace(/[W]+/g, "_"));
}

Max Patch
Copy patch and select New From Clipboard in Max.

I wouldn't like to say which of these solutions uses more (innecessary) processing power however....

Luke Hall's icon

The fourth outlet of [regexp] passes unmatched values so you can use this and the first outlet for your substitutions to cover either scenario.

Emmanuel Jourdan's icon
Max Patch
Copy patch and select New From Clipboard in Max.

That's what I was going to say @luke ;-) The other thing is for simple but clever js like @willyc suggested, you can use jstrigger:

11OLSEN's icon
Max Patch
Copy patch and select New From Clipboard in Max.

i am also doing it with gates. Luke and everybody can you take a look, how do i have to change the regexp that it will handle both cases? if i try to match more than the spaces it will no longer come from outlet1.

O.