Javascript 如何改进regex,使其能够匹配google脚本电子邮件刮板的电子邮件格式?
我正在尝试制作一个电子邮件刮板,可以阅读您的电子邮件,并将交易放入谷歌表格,以便于预算编制 电子邮件的格式如下:Javascript 如何改进regex,使其能够匹配google脚本电子邮件刮板的电子邮件格式?,javascript,regex,Javascript,Regex,我正在尝试制作一个电子邮件刮板,可以阅读您的电子邮件,并将交易放入谷歌表格,以便于预算编制 电子邮件的格式如下: This is an Alert to help you manage your credit card account ending in 0000. As you requested, we are notifying you of any charges over the amount of ($USD) 0.01, as specified in your Alert se
This is an Alert to help you manage your credit card account ending in 0000.
As you requested, we are notifying you of any charges over the amount of ($USD) 0.01, as specified in your Alert settings. A charge of ($USD) 44.44 at UBER * EATS PENDIN has been authorized on Apr 34, 2073 at 2:27 PM ET.
Do not reply to this Alert.
If you have questions, please call the number on the back of your credit card, or send a secure message from your Inbox on www.bank.com.
To see all of the Alerts available to you, or to manage your Alert settings, please log on to www.bank.com.
我试图捕捉价格(44.44)、公司(优步吃)、日期(2073年4月34日)和时间(美国东部时间下午2:27)
我将此作为我的正则表达式:
/A charge of\s\W+\w+\W+\s(.+?(?=at))\w+\s(.+?(?=has))\w+\s\w+\s\w+\s\w+\s(.+?(?=at))\w+\s(.+?(?=ET))/g
然而,尽管它在regex101中匹配,但它不再工作
有没有想过如何让它与谷歌脚本匹配,这样我就可以抓取电子邮件?其他一切都适用于您展示的样品,请尝试以下内容。使用这里的PCRE功能,这将创建3个捕获组,您可以根据需要从中获取值
^(?:As you requested.*\$USD\)\s+)(\d+\.\d+)\s+[\w]+\s+([^ ]*).*?authorized on(.*)\.$
说明:添加上述内容的详细说明
^(?: ##Matching from starting of value, starting a non-capturing group.
As you requested.*\$USD\)\s+ ##Matching string As you requested. till $USD) spaces here.
) ##Closing non-capturing group here.
(\d+\.\d+) ##1st capturing group has digits DOT digits here.
\s+[\w]+\s+ ##Matching spaces word characters spaces here.
([^ ]*) ##2nd capturing group matches till any spaces(basically Uber value will come here).
.*?authorized on ##Matching everything till authorized on here.
(.*)\.$ ##Matching everything till last dot comes of the line, time and date basically.
您的正则表达式在我看来很好,我看到的唯一问题是,您使用的是
global
,无法获得匹配的组。如果你把它取下来,它会好用的。请参阅
您可以在命名组中尝试这样做
const string=`于2073年4月34日美国东部时间下午2:27授权在UBER*EATS PENDIN收取44.44美元的费用';
const regEx=/^已在(?+)\.$/的(?+)授权对(?)处的\s\((?+)\ \s(?\d++.?\d+)进行收费;
console.log(string.match(regEx.groups)
关于您尝试的模式的一些注释
- 您可以省略捕获组中的先行断言
),而将文本作为匹配的一部分(?=
- 最后的断言
将使(?=ET)
不属于该组ET
<> LI>您可以考虑让日期部分更具体(或者至少在稍后验证该部分作为一个有效日期),如接受一个日期,如“代码> APR 34, 2073 < /代码>客户可能永远得不到他的订单。
\bA\s+charge\s+of\s+\D*\b(\d+(?:\.\d+)?)\s+at\s+(\S.*?)\s+has\s+been\s+authorized\s+on\s+(\S.*?)\s+at\s+([^.]+)\.
模式匹配:
Match\bA\s+charge\s+of\s+\D*\b
后跟任何字符,但单词边界之间的数字除外,以防止部分匹配一个charge of
捕获第1组将1+个数字与可选的小数部分匹配(\d+(?:\。\d+)
在空格字符之间匹配\s+at\s+
at
捕获组2匹配一个非空白字符,后跟尽可能多的最后一个字符(\S.*)
匹配\s+已被\s+授权\s+于\s+
已被授权于
捕获组3匹配一个非空白字符,后跟尽可能多的最后一个字符(\S.*)
在空格字符之间匹配\s+at\s+
at
捕获第4组匹配除([^.]+)
以外的1+个字符
匹配一个\。
/g
标志并循环所有组的结果
const regex=/\bA(\D+(?:\.\D+))at(\S.*)的费用已于(\S.*)at([^.]+)\./g获得授权;
const str=`这是一个警报,帮助您管理以0000结尾的信用卡帐户。
根据您的要求,我们将通知您任何超过(\$USD)0.01的费用,如您的警报设置中所指定的。已于2073年4月34日东部时间下午2:27授权在UBER*EATS PENDIN收取(\$USD)44.44的费用。
不要回复此警报。
如果您有任何疑问,请拨打信用卡背面的号码,或从您的收件箱www.bank.com发送安全消息。
要查看所有可用的警报,或管理警报设置,请登录www.bank.com.`;
while((m=regex.exec(str))!==null){
m、 forEach((匹配,i)=>{
如果(i>0)console.log(匹配);
});
}
或使用^(?:根据您的要求。*\$USD\)\s+(\d+(?:\.\d+))\s+[\w]+\s+([^]*)*?在(.*)\.$
上授权覆盖整数并使用正则表达式浮动这两个值。这就是有效的方法!非常感谢。