Google App Script Regex exec()仅在一个函数中返回null [英] Google App Script Regex exec() returns null only in one function
问题描述
我正在撰写Google Apps脚本,以根据我收到的有关作业的自动电子邮件创建日历活动。我正在使用正则表达式来提取我需要在Google日历中填充事件的信息。到目前为止,除了一个函数getEndTime(),它可以找到作业的结束时间,但是在任何时候调用它时都会返回null,所有的功能都按预期运行。我已经阅读了很多关于exec()返回null并且修复了常见问题的其他问题,比如删除了'exec() g'标记并在调用exec()之前将lastIndex重置为0。我也检查了我的正则表达式使用regex101.com与Javascript选项,它显示我期望我的文本匹配。
我的regex表达式在regex101,但不是在我的代码是:
/(Substitute \s + Report\s + Times:\s + [0 -9_] *:[0-9_] * \s + [A-Z_] * \ s + -\s +)([0-9_] *:[0-9_] * \s +(AM | PM) )(\r | \\\
)/
我的代码是:
函数findJobs(){
//在Gmail上搜索替代作业并在日历上创建事件
/ /通过'NewJobs'标签获取电子邮件
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); (var j = 0; j Logger.log(Message+ j);
//以纯文本形式获取电子邮件正文
var body = messages [j] .getPlainBody();
Logger.log(Getting body ...+ j);
//获得学校名称
var school = getSchool(body);
Logger.log(school);
//获取开始时间
var starttime = getStartTime(body);
Logger.log(starttime);
//结束时间
var endtime = getEndTime(body);
Logger.log(endtime);
//获得教师姓名
var teacher = getTeacher(body);
Logger.log(老师);
//获得学校地址
var address = getLocation(body);
Logger.log(address);
//获取日期
var startdate = getDate(body);
Logger.log(startdate);
CalendarApp.getDefaultCalendar()。createEvent(Subbing - + school,new Date(startdate ++ starttime),new Date(startdate ++ endtime),{location:address,描述:teacher});
//threads[j].removeLabel(label);
}
}
Logger.log( - Done--);
}
函数getSchool(文本){
//从任务电子邮件获取学校名称
//校名正则表达式
var regex = /(School \s +:\s +)([a-zA-Z0-9_] *)(\r | \\\
)/;
regex.lastIndex = 0;
var match = regex.exec(text)[2];
返回匹配;
}
函数getDate(text){
//从任务电子邮件获取开始日期
//开始日期的正则表达式
var regex = /(日期:\s +)([0-9_] * \ / [0-9_] * \ / [0-9_] *)(\r | \\\
)/;
regex.lastIndex = 0;
var match = regex.exec(text)[2];
返回匹配;
函数getStartTime(文本){
//从指定电子邮件获取开始时间
//开始时间的正则表达式
var regex = /(Substitute \s + Report\s + Times:\s +)([0-9_] *:[0-9_] * \s +(AM | PM))/;
regex.lastIndex = 0;
var match = regex.exec(text)[2];
返回匹配;
}
函数getEndTime(文本){
//从指定电子邮件获取结束时间
//结束时间的正则表达式
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 | \\\
)/;
regex.lastIndex = 0;
Logger.log(End Time reset index ...);
var match = regex.exec(text)[2];
Logger.log(End Time exec ...);
返回匹配;
}
函数getTeacher(text){
//从任务电子邮件获取教师姓名
//教师姓名$ b的正则表达式$ b var regex = /(Teacher \s +:\s +)([a-zA-Z0-9_] *,[a-zA-Z0-9_] *)(\r | \\\
)/;
regex.lastIndex = 0;
var match = regex.exec(text)[2];
返回匹配;
}
函数getLocation(text){
//从任务电子邮件获取位置
//位置的正则表达式
var regex = /(Address:\s+)(.*)(\r|\
)/;
regex.lastIndex = 0;
var match = regex.exec(text)[2];
返回匹配;
}
以下是我收到的一封邮件:
从9/21/2017开始,您已被指派为替代作业。
以下是工作的详细信息:
*************
作业摘要
********** ***
开始于:9/21/2017
学校:学校网站
标题:学前教师
教师:姓名,教师
替补:姓名,替补
确认#:123456
**********
工作天数
**********
学校
---------------------------------------
学校网站
日期:9/21/2017
员工时间:上午8:00 - 下午3:30
替补报告时间:上午8:00 - 下午3:30
***********************************
学校联系信息
***********************************
学校网站
--- -------------------------------------------------- ------
地址:美国123 Main Ave Anytown 555555
电话:5555555555
-------------------- ---------------------------------------
******* ************* **
特别说明
**********************
请不要回复此系统生成的消息。如果您需要帮助或有其他问题,请发送电子邮件至abc@abc.com
感谢您使用替代分配系统。 Powered by Aesop
复杂。我无法确定是什么原因导致它失败,但我的猜测是最后的(\r | \\\
(请注意,您可以输入
) [\r\\\
,而不是如果你真的想这样做的话。)
]
替代报告时间:。+ - (\d {1,2}:\d {1,2} [) AP] M)
这假定结束时间总是以连字符和空格开头,这看起来是从您提供的示例文本。
I am writing a Google Apps script to create a calendar event based on automated emails I receive for jobs. I am using regex expressions to extract information that I need to populate the event in Google Calendar. So far, I have everything functioning as expected except for one function, getEndTime(), which should find the end time of the job, but presently returns null any time it's called. All of my other functions using exec() work fine.
I have read many other questions regarding exec() returning null and have fixed common issues, such as removing the 'g' tag and resetting the lastIndex to 0 before calling exec(). I have also checked my regex expression using regex101.com with the Javascript option, which shows the match that I expect for my text.
My regex expression that works on regex101, but not in my code is:
/(Substitute\s+Report\s+Times:\s+[0-9_ ]*:[0-9_ ]*\s+[A-Z_ ]*\s+-\s+)([0-9_ ]*:[0-9_ ]*\s+(AM|PM))(\r|\n)/
My code is:
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;
}
Here is an typical email I receive:
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 abc@abc.com
Thank you for using the substitute assignment system. Powered by Aesop
The pattern you're using seems overly complicated. I can't say for sure what's causing it to fail, but my guess would be the (\r|\n)
at the end (note that you can just type [\r\n]
instead if you really want to do this).
Give this pattern a try:
Substitute Report Times:.+ - (\d{1,2}:\d{1,2} [AP]M)
This assumes that the end time is always preceded by a hyphen and a space, which looks to be the case from the sample text you provided.
这篇关于Google App Script Regex exec()仅在一个函数中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!