One of the most powerful feature in JSCAPE MFT Server is Triggers. If you've been using triggers, then you probably already realize how handy they are in automating business processes that involve secure file transfers. However, if you've never employed regular expressions on your triggers, then you're just scratching the surface of this highly versatile feature.
A regular expression is a pattern of characters on which a string of characters is evaluated against to find a match. To understand what we mean, let's have a look at a simple regular expression in action.
Enhancing a trigger with a regular expression
Let me present you a simple scenario where a regular expression can significantly enhance the power of a trigger.
Here's the scenario. When a user uploads a file to your managed file transfer server, you can let the server fire a trigger so that it (the server) can perform certain trigger actions to the uploaded file. For instance, it can encrypt the file or scan the file for viruses.
In most of the trigger-related articles we've posted so far, the actions affected every single uploaded file. Like, in each of those automatic virus scanning articles, scanning was performed on each uploaded file.
But what if you would only like the trigger to act on a specific file, say a file named "somefile.doc"? That's easy. You just specify that in the Trigger Conditions dialog box using the condition:
LocalPath = "somefile.doc"
Any action you associate with this particular trigger will only act on a file named "somefile.doc".
Now, what if you'd like your trigger to act not only on a specific file but on a specific file type; let's say on all files of type .doc. This is when you'll need a regular expression or regex. The regular expression that would match all .doc files is: .*\.doc
So we can rewrite the trigger condition to:
LocalPath ~ ".*\.doc"
If you want to match other file types, then simply change doc with the corresponding extension.
To apply to all jpg files, use: LocalPath ~ ".*\.jpg"
To apply to all txt files, use: LocalPath ~ ".*\.txt"
To apply to all pdf files, use: LocalPath ~ ".*\.pdf"
You get the idea.
Let's add a few slightly more sophisticated examples just to arouse your interest.
To match all pdf filenames beginning with the letters a or b: LocalPath ~ "[ab].*\.pdf"
To match all doc filenames ending with the numbers 3 to 8: LocalPath ~ ".*[3-8]\.doc"
To match all txt filenames beginning with "image" and ending with either letters e to j or numbers 6 to 9: LocalPath ~ "image.*[e-j6-9]\.txt"
Regular expressions in triggers - the basics
Notice that in the first condition we showed you, we used the equal sign (=). But in succeeding conditions, we replaced it with the tilde (~). You use the ~ operator if you want to evaluate the value of the variable on the left (in this case, LocalPath) against a regular expression on the right. If they match, the entire trigger condition will evaluate to TRUE and the trigger actions associated with that trigger will execute.
When used in this context, regular expressions are enclosed in double quotes. For example, in the trigger condition LocalPath ~ ".*\.doc", the regular expression is: .*\.doc
Regular Expressions is a broad topic by itself but you really don't need to know everything under it to make the most out of JSCAPE managed file transfer server triggers. In fact, if you can understand the sample regex above, you'll already be able to construct some pretty handy triggers. In this post we'll tackle that particular regex and introduce you to a few more sophisticated ones.
Let me partially dissect that regex above so you can see how easy regular expressions really can be.
Let's start with the most basic components of a regular expression: the ordinary characters or literal characters. In our first sample regex, the characters '.','d','o', and 'c' of '.doc' are literal characters. Meaning, our application is going to interpret them exactly as they appear. In simple terms, literally.
But there are other characters in that regex. Those characters, i.e., '.','*', and '\', are not literal characters. They are special characters. More formally known as metacharacters, these special characters have special functions.
Wait a minute. Didn't I earlier say the '.' was a literal character? Well, it can be a literal character. That is, if you place a backslash (\) right before it. Any metacharacter that is placed right after a '\' will be treated as a literal character.
So now you know what '\' does. It forces metacharacters to be treated as literal characters. Actually, the '\', when combined with certain characters (but not metacharacters), can do other things but let's not get to that yet.
But how can you tell when a character is a literal character or not? Generally speaking, unless you see a non-alphanumeric character enclosing it or immediately right beside it, an alphanumeric character is interpreted as a literal character. Don't worry. I'll start with very simple examples, so it would be easy to tell which ones are literal characters and which ones are not.
Then as we move on, I'll slowly introduce you to widely used metacharacters so you can see alphanumeric characters that no longer act as literal characters.
Already this early in our discussion, we can construct a simple regex using literal characters and the only metacharacter we know, '\'.
Remember this trigger condition?
LocalPath = "somefile.doc"
The expression on the right of the equal sign is not a regular expression but we can actually rewrite that into one. Here it is:
LocalPath ~ "somefile\.doc"
To see whether these two conditions really have the same effect, let's run them through JSCAPE MFT Server's built-in expression tester.
Testing regular expressions
Let's start with the first condition. Enter it into the Expression box and then click Test Expression.
That should bring up the Test Expression dialog box.
Enter a value in the LocalPath field and click Test. For example, if you'd like to see what would happen if the uploaded file is named "samplefile.doc", enter that filename into the LocalPath field. After that, click Test.
This will prompt the server to compare the value stored in the LocalPath variable (which is taken from the LocalPath field) and the expression you entered earlier. If a match is found, the condition will evaluate to TRUE. If not, it will evaluate to FALSE.
Because "samplefile.doc" does not match our expression "somefile.doc", the condition evaluates to FALSE.
Try entering other values into the LocalPath field. You'll see that the only value that would allow the condition to evaluate to TRUE is "somefile.doc". All other values (meaning, all other filenames) should evaluate to FALSE.
Now to test our regular expression. Enter: LocalPath ~ "somefile\.doc" into the Expression box and follow the same steps we took when testing the other expression.
If you did everything correctly, the only entry that would evaluate to TRUE should be "somefile.doc". All others should evaluate to FALSE.
The results show that the condition (which uses a simple regular expression):
LocalPath ~ "somefile\.doc"
has the same effect as:
LocalPath = "somefile.doc".
Of course, this doesn't showcase the power of regular expressions. We're still getting warmed up. I had to teach you how to test regular expressions as we'll be doing that often later in this post.
In this article we provided a basic introduction to regular expressions and how they may be used in triggers. In Part 2 of this post, we'll continue dissecting the regular expression " .*\.doc". Remember that we still haven't talked about the two metacharacters '.' and '*', so we'll do that there.