How to use Regular Expression (Regex)
Summary
Regular Expressions(Regex) can be complicated to create, however, JEMH does not utilise all of the features that Regex offers as JEMH only uses it is for simple pattern matching. JEMH uses Regex for:
Body Delimiters and Cleanups - This is to remove content from the email body.
Server/Dc page: Use Project Mapping Cleanup and Body Delimiters
Cloud page: Applying Body Regexes
Subject IssueKey (comment) Regexps - Check if a IssueKey is present in the subject of the email when commenting.
Server/Dc page: Use Subject Issue Key Matching
Cloud page, see the Issue key in subject section: Understand how Issue association works | Issue key in subject
Regexp Field Processor - Retrieve values from email content to be added as a value for a Custom Field.
Server/Dc page: Use Regexp Field Processor
Cloud page: Use Regexp Field Processor
This guide cover how to create simple pattern matches and how to use special characters within Regex.
Pattern Matching
Pattern Matching will search for content that matches the Regex exactly. There are multiple different ways that Regex can extract information from the email. Some ways are:
Matching a Static value
Regex can match a static value which has been entered. For example if you want to match the word “Hello” within a email, the Regex will look like:
Hello
The example content below contains the word “Hello” at the start of the line which will return a match:
Hello world, I am a Example email.
Matching Dynamic values
The values you will look for might not always be a static value and this means that you must utilise character classes as this will allow you to look for a value that is of a certain character type.
This example is looking for a IssueKey that is within the AB project. Regex will dynamically look for the number value this will not always be the same.
\[AB-[0-9]+\]
The example subject below will return a value of [AB-34]:
Capture groups
Capture groups are used to retrieve a value from the email body to then be used as a value for a Custom Field. To create a capture group put “( )” around the characters that you are looking to extract.
This example is looking for the words “Foreign Key= “ and if found it is adding the following words into the value of a Custom Field.
The example email body will return the value of “example value”.
Escaping Special characters
To match special characters with a Regex you must use “\” to escape these characters. If you would like to escape “\” you would need to put a “\” before this as well which will make it look like “\\”.
Below is a Regex example that escapes “[ ]” so that it will be included when the value is extracted:
The example content below will return [tb-53]:
Special Characters
Regex uses special characters to perform certain functions. Some Special characters that will help with creating expressions are:
Special character | Function |
---|---|
. | This will match any single character other than a newline |
* | This matches zero or more consecutive characters. For example (/ba*) will match “ba” and “baaa” as the expression will match more than one “a” characters. |
\ | Escape following special character. This will allow special characters to be matched within the email. For example if you are looking for “[abc]” the regex will have to be \[abc\] as this will mean that the regex will also look for “[ ]” as a character. |
( ) | Used to define a capture group within a regex. |
[ ] | This is used to define which Character sets you want to capture. For example ([a-z]) will capture all characters that are a lower case alphabetical character. |
{ } | Indicates how many characters the value must have. This can be an exact number or between a certain range. |
+ | Matches one or more consecutive characters. |
| | Matches either what is before or after the | allowing for a or condition |
In regards to the expression .*
The expression is a wildcard it’s usage should be restricted and only when completely necessary. This expression will match everything. If used incorrectly this can cause long processing times due to the expression evaluating a possibly long input which significantly delays processing in JEMH Data Center/JEMH Cloud.