RegEx

Post Reply
User avatar
Terkelsen
Advanced member
Posts: 297
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

RegEx

Post 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?
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: RegEx

Post 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.
User avatar
Terkelsen
Advanced member
Posts: 297
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post 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!
User avatar
Terkelsen
Advanced member
Posts: 297
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post by Terkelsen »

By the way. Is there any way to search for a quotation mark (") as well?
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: RegEx

Post 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("");
User avatar
Terkelsen
Advanced member
Posts: 297
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post by Terkelsen »

Great, Padawan! Thanks again :-)
Post Reply