使用 Google Apps 脚本和正则表达式在其他两个字符串之间抓取字符串
Grabbing string between two other strings using Google Apps Scripts and Regular Expressions
我正在使用 Google Apps 脚本来解析邮箱。
我在识别要解析的消息时没有遇到任何问题,当我想从消息的 plainBody 中获取特定的文本块时,问题就来了。
Plain Body 的摘录:
Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain domain.com by aspmx.l.google.com. [173.194.77.26].
The error that the other server returned was:
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 http://support.google.com/mail/bin/answer.py?answer=6596 y3si2744337oex.60 - gsmtp
----- Original message -----
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20130820;
我正在尝试获取所有以 550 开头的行(至少,在本例中它们以 550 开头,可以是这些行开头的任意数量的代码)
我用来尝试获取它的代码是
var bodyRegex = new RegExp("The error that the other server returned was:[\s\S]*----- Original message -----");
Logger.log(bodyRegEx.exec(plainBody));
它只是返回 null
使用 RegExLib 我能够成功使用这个 RegEx
因为您正在编译 RegExp 对象,请尝试转义您的斜杠:
var bodyRegex = new RegExp("The error that the other server returned was:[\s\S]*----- Original message -----");
或者直接使用文字:
var bodyRegex = /The error that the other server returned was:[\s\S]*----- Original message -----/;
我正在使用 Google Apps 脚本来解析邮箱。 我在识别要解析的消息时没有遇到任何问题,当我想从消息的 plainBody 中获取特定的文本块时,问题就来了。
Plain Body 的摘录:
Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain domain.com by aspmx.l.google.com. [173.194.77.26].
The error that the other server returned was:
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 http://support.google.com/mail/bin/answer.py?answer=6596 y3si2744337oex.60 - gsmtp
----- Original message -----
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20130820;
我正在尝试获取所有以 550 开头的行(至少,在本例中它们以 550 开头,可以是这些行开头的任意数量的代码)
我用来尝试获取它的代码是
var bodyRegex = new RegExp("The error that the other server returned was:[\s\S]*----- Original message -----");
Logger.log(bodyRegEx.exec(plainBody));
它只是返回 null
使用 RegExLib 我能够成功使用这个 RegEx
因为您正在编译 RegExp 对象,请尝试转义您的斜杠:
var bodyRegex = new RegExp("The error that the other server returned was:[\s\S]*----- Original message -----");
或者直接使用文字:
var bodyRegex = /The error that the other server returned was:[\s\S]*----- Original message -----/;