Skip to content Skip to sidebar Skip to footer

Regex - Extract A Substring Of A Email Document With A Regular Expression

I'm trying to extract a substring of a email document with a regular expression. I'm testing the regex online, and it works perfectly: online regex tester I have a function to che

Solution 1:

Using a literal regex instead of converting the string to a regex works.

var regex = newRegExp(/Intrusion signature\(s\)\:\n\n(.*)/);

enter image description here

Solution 2:

When you use regular strings with RegExp you need to escape every backslash. Here are some alternatives you can use. Another aspect to consider is that if you have CR+LF in your Spreasheet. In that case you need to change your code to include \r I've added some google API mocked code so that the code snippet can run here

// -----------------  Ignore this (Mock)Logger=console;
class_SpreadsheetApp { getActiveSpreadsheet() {  classSpreadSheet {    getSheetByName(name) {
 classSheet { getRange() { classRange { getValue() {
            return"  * Intrusion signature(s):\r\n\r\n      - This is the text I want to extract.\r\n\r\n * SI communication:\r\n";
} } returnnewRange(); } } returnnewSheet(); } } returnnewSpreadSheet(); } }
varSpreadsheetApp=new_SpreadsheetApp();
// ----------------- Ignore this (Mock)functioncheckRegExp() {
  var data=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LOG").getRange("C213").getValue();
  var regex = newRegExp("Intrusion signature\(s\)\:\n\n(.*)");  // ORIGINALvar e = regex.exec(data);
  Logger.log("Original:"+e);

  var sol1 = newRegExp("Intrusion signature\\(s\\)\\:\\r\\n\\r\\n(.*)");
  e = sol1.exec(data); 
  if (e) Logger.log("Solution 1:"+e[1]);

  var sol2 = newRegExp("Intrusion signature\\(s\\)\\:\\W{2,4}(.*)");
  e = sol2.exec(data); 
  if (e) Logger.log("Solution 2:"+e[1]);

  var sol3 = newRegExp("Intrusion signature\\(s\\)\\:(?:\\x0d\\x0a)+(.*)");
  e = sol3.exec(data); 
  if (e) Logger.log("Solution 3:"+e[1]);

  var sol4 =   newRegExp(/Intrusion signature\(s\)\:\r\n\r\n(.*)/);
  e = sol4.exec(data); 
  if (e) Logger.log("Solution 4:"+e[1]);
  
}

checkRegExp()

Post a Comment for "Regex - Extract A Substring Of A Email Document With A Regular Expression"