interesting regexp problem

Jan 23, 2012 at 4:18am

interesting regexp problem

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!

#61393
Jan 23, 2012 at 7:50am

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, “_”));
}

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

– Pasted Max Patch, click to expand. –
#221436
Jan 23, 2012 at 11:14am

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

#221437
Jan 23, 2012 at 11:26am

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:

– Pasted Max Patch, click to expand. –
#221438
Jan 23, 2012 at 11:45am

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.

– Pasted Max Patch, click to expand. –

O.

#221439

You must be logged in to reply to this topic.