2017-09-25 48 views
0

我正在編寫一個Google Apps腳本來基於我收到的有關作業的自動電子郵件創建日曆事件。我正在使用正則表達式來提取我需要在Google日曆中填充事件的信息。到目前爲止,除了一個函數getEndTime(),它可以找到作業的結束時間,但是在任何時候調用它時都會返回null,所有的功能都按預期運行。我所有使用exec()的其他函數都可以正常工作。Google App腳本Regex exec()僅在一個函數中返回null

我已經閱讀了很多關於exec()返回null並解決了常見問題的其他問題,比如在調用exec()之前刪除'g'標記並將lastIndex重置爲0。我還用Javascript選項使用regex101.com檢查了我的正則表達式,該選項顯示了我期望的文本匹配。

上regex101工作,但不是在我的代碼我的正則表達式的表達式爲:

/(Substitute\s+Report\s+Times:\s+[0-9_ ]*:[0-9_ ]*\s+[A-Z_ ]*\s+-\s+)([0-9_ ]*:[0-9_ ]*\s+(AM|PM))(\r|\n)/ 

我的代碼是:

function findJobs() { 
//Searches Gmail for substitute jobs and creates an event on the calendar 

    //Gets emails with 'NewJobs' label 
    var label = GmailApp.getUserLabelByName("NewJobs"); 
    var threads = label.getThreads(); 
    for (var i = 0; i < threads.length; i++){ 

    var messages = threads[i].getMessages(); 
    Logger.log("Thread " + i); 

    for (var j = 0; j < messages.length; j++) { 
     Logger.log("Message " + j); 

     //gets email body in plain text 
     var body = messages[j].getPlainBody(); 
     Logger.log("Getting body..." + j); 

     //gets school name 
     var school = getSchool(body); 
     Logger.log(school); 

     //gets start time 
     var starttime = getStartTime(body); 
     Logger.log(starttime); 

     //gets end time 
     var endtime = getEndTime(body); 
     Logger.log(endtime); 

     //gets teacher name 
     var teacher = getTeacher(body); 
     Logger.log(teacher); 

     //gets school address 
     var address = getLocation(body); 
     Logger.log(address); 

     //gets date 
     var startdate = getDate(body); 
     Logger.log(startdate); 

     CalendarApp.getDefaultCalendar().createEvent("Subbing - " + school, new Date(startdate + " " + starttime), new Date(startdate + " " + endtime), {location: address, description: teacher}); 
     //threads[j].removeLabel(label); 
    } 
    } 
    Logger.log("--Done--"); 
} 

function getSchool(text){ 
    //Gets the school name from an assignment email 

    //Regular expression for school name 
    var regex = /(School\s+:\s+)([a-zA-Z0-9_ ]*)(\r|\n)/; 
    regex.lastIndex = 0; 
    var match = regex.exec(text)[2]; 

    return match; 
} 

function getDate(text){ 
    //Gets the start date from an assignment email 

    //Regular expression for start date 
    var regex = /(Date:\s+)([0-9_ ]*\/[0-9_ ]*\/[0-9_ ]*)(\r|\n)/; 
    regex.lastIndex = 0; 
    var match = regex.exec(text)[2]; 

    return match; 
} 

function getStartTime(text){ 
    //Gets the start time from an assignment email 

    //Regular expression for start time 
    var regex = /(Substitute\s+Report\s+Times:\s+)([0-9_ ]*:[0-9_ ]*\s+(AM|PM))/; 
    regex.lastIndex = 0; 
    var match = regex.exec(text)[2]; 

    return match; 
} 

function getEndTime(text){ 
    //Gets the end time from an assignment email 

    //Regular expression for end time 
    var regex = /(Substitute\s+Report\s+Times:\s+[0-9_ ]*:[0-9_ ]*\s+[A-Z_ ]*\s+-\s+)([0-9_ ]*:[0-9_ ]*\s+(AM|PM))(\r|\n)/; 
    regex.lastIndex = 0; 
    Logger.log("End Time reset index..."); 
    var match = regex.exec(text)[2]; 
    Logger.log("End Time exec..."); 

    return match; 
} 

function getTeacher(text){ 
    //Gets the teacher name from an assignment email 

    //Regular expression for teacher name 
    var regex = /(Teacher\s+:\s+)([a-zA-Z0-9_ ]*,[a-zA-Z0-9_ ]*)(\r|\n)/; 
    regex.lastIndex = 0; 
    var match = regex.exec(text)[2]; 

    return match; 
} 

function getLocation(text){ 
    //Gets the location from an assignment email 

    //Regular expression for location 
    var regex = /(Address:\s+)(.*)(\r|\n)/; 
    regex.lastIndex = 0; 
    var match = regex.exec(text)[2]; 

    return match; 
} 

下面是一個典型的電子郵件我收到:

You have been assigned as a substitute for a job starting on 9/21/2017. 
The following are the details of the job: 
************* 
Job Summary 
************* 
Starting On    : 9/21/2017 
School      : School Site 
Title      : Pre School Teacher 
Teacher     : Name, Teacher 
Substitute     : Name, Substitute 
Confirmation #    : 123456 

********** 
Job Days 
********** 
School 

--------------------------------------- 
School Site 
Date: 9/21/2017 
Employee Times: 8:00 AM - 3:30 PM 
Substitute Report Times: 8:00 AM - 3:30 PM 

*********************************** 
School Contact Information 
*********************************** 
School Site 
----------------------------------------------------------- 
Address: 123 Main Ave Anytown , USA 555555 
Phone: 5555555555 
----------------------------------------------------------- 
********************** 
Special Instructions 
********************** 



Please do not reply to this system generated message. If you need help or have additional questions, please send an email to [email protected] 

Thank you for using the substitute assignment system. Powered by Aesop 

回答

0

您使用的圖案看起來過於複雜。我無法確定是什麼原因導致它失敗,但我的猜測是最後的(\r|\n)(注意,如果你真的想這樣做,你可以輸入[\r\n])。

給這個模式的嘗試:

Substitute Report Times:.+ - (\d{1,2}:\d{1,2} [AP]M)

這假設結束時間總是一個連字符和空格,這看起來是從您提供的樣本文本的情況之前。

+0

你的模式工作!我確實必須將索引從2更改爲1,但它工作正常!出於好奇,我嘗試用[\ r \ n]替換(\ r | \ n),但我仍然得到相同的錯誤。我將研究你的替代模式,看看我還能從中學到什麼。謝謝! –

+0

對不起,我並不是想說''[\ r \ n]'會解決'(\ r | \ n)'造成的問題;只是它們在匹配的方面是同義詞,但前者更簡單,成本更低,因爲它不會創建捕獲組。無論何時,只要匹配一組單個字符中的一個,方括號就是要走的路(有一些例外)。 – CAustin

+0

好吧,明白了!我仍然無法弄清楚我以前的模式有什麼問題,但至少現在正在工作,並且我已經學到了一些關於更好的格式化正則表達式的方法。再次感謝! –

相關問題