- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-01-2020 03:47 PM
I am trying to parse through an incoming email using pairs like "foo:bar" but it has come to my attention that some of the different users emailing us are changing parts of their terms. I would like to change this into regex to try and still capture some of them. I am using this below. Can someone help me with creating a regex for "title"? With a single example I'm confident I can do the rest of them. An example of what I am getting now is "infringer's title: the change;", so I would like to try and focus on title to grab "the change".
// Note: current.opened_by is already set to the first UserID that matches the From: email address
var bodyText = email.body_text;
if (!bodyText)
bodyText = email.body_html;
// Strip off the FW:
var subjectLine = email.subject;
if (subjectLine.toLowerCase().indexOf("fw:") == 0)
subjectLine = subjectLine.substring(3).trim();
current.work_notes = "xca Allegation MGMT Request created by email forwarded by: " + email.origemail + "\n\n" + email.subject + "\n\n" + bodyText;
current.short_description = subjectLine;
current.x_unoci_xca_alleg_universal_id = email.body.case_id;
current.x_unoci_xca_alleg_infringed_work = email.body.title;
current.x_unoci_xca_alleg_ip_address = email.body.ip_address;
current.x_unoci_xca_alleg_port_number_used = email.body.port;
current.x_unoci_xca_alleg_protocol_used = email.body.type;
current.x_unoci_xca_alleg_file_name = email.body.filename;
current.x_unoci_xca_alleg_file_size = email.body.filesize;
current.x_unoci_xca_alleg_time_stamp = email.body.timestamp;
current.state = "10";
var newId = current.insert();
// <stop_processing>true</stop_processing>
gs.eventQueue('damr.request.email.creation', current, newId, email.from);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 03:45 PM
Hi Chris,
No worries - so there are (at least) two ways to handle this and it comes down to how you handle the output. If you're checking different strings for the same key, what I'd recommend would be alternation. If you were checking for different keys to get their value, you could still use a single regex but you'd need to iterate through the output against your key values. So I'll put both examples below:
// Example one, where you have multiple possible keys but want to use the same regex
var matchTitle = new RegExp(/(title:\s|topic:\s|issue:\s)(\w*[^\S\r\n]*)*/i);
var keyString = ["title:", "topic:", "issue:"];
var valString = "";
// Run the text through the established regex
var matchTitleCheck = matchTitle.exec(email.body);
// If the variable is not nil, it is an array containing instances of regex
if (!JSUtil.nil(matchTitleCheck)) {
// Grab the first instance (only instance), and check which string you're matching to then remove it's length (makes sense for different lengths)
for (var i = 0; i < keyString.length; i++) {
if (matchTitleCheck[0].indexOf(keyString[i]) > -1) {
valString = matchTitleCheck[0].substring(keyString[i].length).trim();
}
}
}
// Example two, where you are looking for different keys but want to use the same regex
// This time we use the global flag and the case insensitive flag, global will mean we return more than one result into our executed array
var matchTitle = new RegExp(/(title:\s|category:\s|priority:\s)(\w*[^\S\r\n]*)*/gi);
var keyString = ["title:", "category:", "priority:"];
var valString = "";
// Run the text through the established regex
var matchTitleCheck = matchTitle.exec(email.body);
// If the variable is not nil, it is an array containing instances of regex
if (!JSUtil.nil(matchTitleCheck)) {
// Since we potentially have multiple entries in the matchTitleCheck array, we need to iterate across them AND the keyString.
// You could do switch checks to be faster here, the nested loops are an example only because I have a shortcut and not a recommendation
for (var i = 0; i < matchTitleCheck.length; i++) {
for (var j = 0; j < keyString.length; j++) {
if (matchTitleCheck[i].indexOf(keyString[j]) > -1) {
valString = matchTitleCheck[0].substring(keyString[j].length).trim();
}
}
}
}
// The alternative is that you can declare multiple regular expressions, and execute them across the same string then use the output, and that is a faster solution generally
var matchCat = new RegExp(/(category:\s)(\w*[^\S\r\n]*)*/i);
var matchPri = new RegExp(/(priority:\s)(\w*[^\S\r\n]*)*/i);
var keywords = {cat: "category:", pri: "priority:"};
var category = "";
// Since non-globals will give us one match, you would be able to use the original example of if not nil, grab the first element of the array and extract the appropriate element
// You can use an object like keywords to make this simpler, so you have:
var matchCatChk = matchCat.exec(email.body);
if(!JSUtil.nil(matchCatChk)){
category = matchCatChk[0].substring(keywords.cat.length).trim();
}
I hope this help clarify a way forward - to consolidate, you can use multiple alternated (via | character) searches in one regex or use separate searches, with their own performance considerations.
Kind Regards,
Astrid

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-01-2020 04:58 PM
Hi,
What you're looking for is something like:
// Establish regular expression and filter
var matchTitle = new RegExp(/(title:\s)(\w*[^\S\r\n]*)*/i);
var keyString = "title:";
// Run the text through the established regex
var matchTitleCheck = matchTitle.exec(email.body);
// If the variable is not nil, it is an array containing instances of regex
if(!JSUtil.nil(matchTitleCheck)){
// Grab the first instance (only instance), and remove the leading 'title:' by substring
// Then trim the leading and tailing whitespace
var valString = matchTitleCheck[0].substring(keyString.length).trim();
}
The breakdown of the regex is: matching your initial key, and then matching for n of word characters and n of whitespace characters that are not newline characters. The in flag has been used to ensure 'title' is read case-insensitively.
Let me know if this helps, or if you need any further information or clarity?
Kind regards,
Astrid Sapphire
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 02:01 PM
Hello Astrid,
Thank you so much for replying, terribly sorry it took some time for me to respond. I have been trying to construct an expression to handle multiple values to be input into different fields, but I'm going to be honest I am no expert for regex. Most of my experience is client scripts for filtering. If I can rely on your expertise a bit more could you show me what a regex expression looking for 2 or 3 values looks like? I think I would be good past that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 03:45 PM
Hi Chris,
No worries - so there are (at least) two ways to handle this and it comes down to how you handle the output. If you're checking different strings for the same key, what I'd recommend would be alternation. If you were checking for different keys to get their value, you could still use a single regex but you'd need to iterate through the output against your key values. So I'll put both examples below:
// Example one, where you have multiple possible keys but want to use the same regex
var matchTitle = new RegExp(/(title:\s|topic:\s|issue:\s)(\w*[^\S\r\n]*)*/i);
var keyString = ["title:", "topic:", "issue:"];
var valString = "";
// Run the text through the established regex
var matchTitleCheck = matchTitle.exec(email.body);
// If the variable is not nil, it is an array containing instances of regex
if (!JSUtil.nil(matchTitleCheck)) {
// Grab the first instance (only instance), and check which string you're matching to then remove it's length (makes sense for different lengths)
for (var i = 0; i < keyString.length; i++) {
if (matchTitleCheck[0].indexOf(keyString[i]) > -1) {
valString = matchTitleCheck[0].substring(keyString[i].length).trim();
}
}
}
// Example two, where you are looking for different keys but want to use the same regex
// This time we use the global flag and the case insensitive flag, global will mean we return more than one result into our executed array
var matchTitle = new RegExp(/(title:\s|category:\s|priority:\s)(\w*[^\S\r\n]*)*/gi);
var keyString = ["title:", "category:", "priority:"];
var valString = "";
// Run the text through the established regex
var matchTitleCheck = matchTitle.exec(email.body);
// If the variable is not nil, it is an array containing instances of regex
if (!JSUtil.nil(matchTitleCheck)) {
// Since we potentially have multiple entries in the matchTitleCheck array, we need to iterate across them AND the keyString.
// You could do switch checks to be faster here, the nested loops are an example only because I have a shortcut and not a recommendation
for (var i = 0; i < matchTitleCheck.length; i++) {
for (var j = 0; j < keyString.length; j++) {
if (matchTitleCheck[i].indexOf(keyString[j]) > -1) {
valString = matchTitleCheck[0].substring(keyString[j].length).trim();
}
}
}
}
// The alternative is that you can declare multiple regular expressions, and execute them across the same string then use the output, and that is a faster solution generally
var matchCat = new RegExp(/(category:\s)(\w*[^\S\r\n]*)*/i);
var matchPri = new RegExp(/(priority:\s)(\w*[^\S\r\n]*)*/i);
var keywords = {cat: "category:", pri: "priority:"};
var category = "";
// Since non-globals will give us one match, you would be able to use the original example of if not nil, grab the first element of the array and extract the appropriate element
// You can use an object like keywords to make this simpler, so you have:
var matchCatChk = matchCat.exec(email.body);
if(!JSUtil.nil(matchCatChk)){
category = matchCatChk[0].substring(keywords.cat.length).trim();
}
I hope this help clarify a way forward - to consolidate, you can use multiple alternated (via | character) searches in one regex or use separate searches, with their own performance considerations.
Kind Regards,
Astrid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2020 03:05 PM
Thank you very much for the help Astrid, it was very informative and I think I learned a lot. I especially appreciate the notation in the scripts. Again thank you for the help!