RegEx

Post Reply
User avatar
Terkelsen
Member
Posts: 165
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

RegEx

Post by Terkelsen » Thu May 31, 2018 8:46 am

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
Member
Posts: 92
Joined: Mon Jun 12, 2017 8:48 pm

Re: RegEx

Post by Padawan » Thu May 31, 2018 10:50 am

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
Member
Posts: 165
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post by Terkelsen » Fri Jun 01, 2018 10:29 am

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
Member
Posts: 165
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post by Terkelsen » Fri Jun 01, 2018 11:42 am

By the way. Is there any way to search for a quotation mark (") as well?

Padawan
Member
Posts: 92
Joined: Mon Jun 12, 2017 8:48 pm

Re: RegEx

Post by Padawan » Fri Jun 01, 2018 1:33 pm

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
Member
Posts: 165
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: RegEx

Post by Terkelsen » Mon Jun 04, 2018 2:48 pm

Great, Padawan! Thanks again :-)

Post Reply