Guide to Write Regular Expression Rules in PopTray (Part 1)
Understanding what is a Reg Expr
Your rules may currently use one of the following test conditions that PopTray provides:
- Contains
- Equals
- Wildcard
- Empty
All this conditions can be rewritten using the new Reg Expr, as I will show you now. But you must read this whole article to get the main idea and start writing your own patterns. If you just pick some hints, you will have to come back again to find how to write a new rule... this is not the idea.
Patterns are the Regular Expression's search string. By the way, PopTray's patterns syntax are a subset of a major Regular Expression package, so what you learn here will also work on other applications, programming languages, ... [
Link to RegExpStudio has been removed since it now tries to download malware, an internet search may give the same info but be careful since malware is being spread on this site, it's likely on others also ] also gives some other extensions which may not work elsewhere.
If a whole pattern can be applied to a
continuous portion of a text area like an email subject, we will say that the Reg Expr succeed, and the rule actions will be fired.
Usually, patterns are case sensitive, but as Text area in PopTray rules is case insensitive for all condition types, patterns written in PopTray will also be case insensitive for Reg Expr
Contains
Contains is the simplest Regular Expression. It only tests if a given string is contained inside a section of the email.
You can replace the rule:
From (Name) | Contains | Renier
with:
From (Name) | Reg Expr | Renier
giving the same result. Congratulations, you wrote your first Regular Expression pattern in a rule
Equals
This is a condition that forces a text field to match exactly the string you specified in the rule.
For example, your rule:
To | Equals | my_account@yahoo.com
will not catch "
my_account@yahoo.com.es" alternative email as expected.
The corresponding Reg Expr to Equals should be:
To | Reg Expr | ^my_account@yahoo.com$
Two new elements where introduced here:
- A beginning "
^" means that the text should start with the string following it.
- An ending "
$" means that the text should finish with the string preceding it.
You can place a "
$" elsewhere in the pattern to search for a dollar sign, but what if I want to match an ending "
$"? Just precede it with a "
\" (backslash, the escape character without the quotes).
Then, if your rule says:
To | Reg Expr | ^my_account@yahoo.com
will also match "
my_account@yahoo.com.es", "
my_account@yahoo.com.mx", ...
Empty
Did you really understand the meaning of "
^" and "
$". Well, so what does a "
^$" pattern mean for a Regular Expression? Guess! Right, it means "
Nothing between the begin and the end of the text", i.e., and empty field.
So,
Subject | Empty |
becomes:
Subject | Reg Expr | ^$
and you will find messages without a subject.
Wildcard
Wildcards are quite familiar to us. We used them in Apple, Atari and MS-DOS age to list "*.BAS" programs, "PHOTO???.PIC" images, and so on. If you have not used them, I'll explain them here:
- "?" matches exactly one alphabetic char, number or whatever sign you may type using your keyboard or other input device.
- "*" matches zero or more chars... usually means "
whatever it says".
Obviously, this two wildcards have a corresponding notation in a Regular Expression:
- "
." (dot) is a replacement for "?" wildcard.
- "
.*" (dot-asterisk) is a replacement for "*" wildcard.
Very simple... Now you can rewrite your rule:
Subject | Wildcard | *buy*online*
where the middle "*" replaces any product name, as:
Subject | Reg Expr | buy.*online
Note that I omit the leading and trailing "*" because, if you remember the
Contains type, you don't need to specify anything around the pattern, unless you want to check one or both ends of the text using "
^" and "
$".
And that's all, folks! Now, you can rewrite all your rules as Regular Expressions.
But wait!!! Someone told you that Regular Expressions are powerful and you see nothing new in the preceding paragraphs? That's because all you write in your rules are constant strings and wildcards, but in Reg Expr patterns you can write more useful wildcards and use classes of chars instead of constants.
++Vitoco