Page 1 of 1

RegEx

Posted: Thu May 31, 2018 8:46 am
by Terkelsen
I could use a little help with a RegEx expression.

I a Switch flow we are renaming files by using the e-mail subject attached to the file. The subject may contain characters like :?!^<>\ which are considered illegal characters to be used in a filename.

I want af RegEx to sort out everything but these characters, put this in Private data and then use this Private Data information to rename the files.

I'm using this RegEx: [^(?!.*:^<>)]

Testet on regex101.com this will do this T*e:s<t = Test

However, when used in Switch it seem to only return the first character, so T*e:s<t = T

Any ideas?

Re: RegEx

Posted: Thu May 31, 2018 10:50 am
by Padawan
If you test it on regex101.com, then on the right you can see your Match information. This shows that the first match is "T", the second match is "e", and so on:

Image

I assume you are using the "Search" string manipulation in Switch? This will always only return the first match it finds, which is why you only get "T".

I'm afraid the only option I see is a script. Something like this:

Code: Select all

//var input = job.getVariableAsString("[Email.Subject]");
var input = "T*e:s<t";
var regExp = /[^(?!.*:^<>)]/g;
var result = input.match(regExp);
result.join("");
This will get all the matches in a string array and then join the string array together.

Re: RegEx

Posted: Fri Jun 01, 2018 10:29 am
by Terkelsen
Hi Padawan,

You are absolutely right. I'm trying to use the "Search" string manipulation in Switch and it only returns the first match.

However, your script works perfect! I can use that with a Rename element and this way I don't even have to bother putting stuff into Private data or anything.

Thanks a lot!

Re: RegEx

Posted: Fri Jun 01, 2018 11:42 am
by Terkelsen
By the way. Is there any way to search for a quotation mark (") as well?

Re: RegEx

Posted: Fri Jun 01, 2018 1:33 pm
by Padawan
You're welcome. And yes you can, you just need to escape the " with a \ in the regex:

Code: Select all

//var input = job.getVariableAsString("[Email.Subject]");
var input = "T\"\'*e:s<t";
var regExp = /[^(\"?\'!.*:^<>)]/g;
var result = input.match(regExp);
result.join("");

Re: RegEx

Posted: Mon Jun 04, 2018 2:48 pm
by Terkelsen
Great, Padawan! Thanks again :-)