Google脚本:将RegEx匹配为2D数组 [英] Google Script: Match RegEx into 2D array
问题描述
我正在尝试将Gmail中的信息提取到Google Spreadsheet中.电子邮件中的信息具有表结构,其中包含以下列产品清单,已售数量和每种产品的小计.这些重复N次.
使用 message.getPlainBody()
访问信息时,我得到以下文本:
产品数量价格巧克力1个$ 8.58苹果2个$ 40.40香蕉1个$ 95.99糖果1个$ 4.99小计:$ 149.96
进度
首先,我尝试使用正则表达式来标识每一行及其所有元素:
- 产品名称:不包含':'(.*)[^:] 的任意数量的字符
- 售出数量:任何数字\ d *
- 任何看起来像小计[$] \ d *.\ d *
将所有内容包装成这样
函数ExtractDetail(message){var主体= message.getPlainBody();//正则表达式var itemListRegex = new RegExp(/(.*)[^:] [\ r \ n] +(\ d * [\ r \ n] + [$](\ d * \.\ d *)[\ r \n] +/g);var itemList = mainbody.match(itemListRegex);Logger.log(itemList);}
到目前为止,它仍然有效:
商品清单:巧克力1 $ 8.58,苹果2 $ 40.40,香蕉1 $ 95.99,糖果1 $ 4.99
但是,我得到以下结果:
- [巧克力1 $ 8.58]
- [Apples 2 $ 40.40]
- [香蕉1 $ 95.99]
- [糖果1 $ 4.99]
代替:
- [巧克力] [1] [$ 8.58]
- [苹果] [2] [$ 40.40]
- [香蕉] [1] [$ 95.99]
- [糖果] [1] [$ 4.99]
问题
我的问题是,如何以每行对应于找到的每个匹配项以及每列对应于每个属性的方式追加新行?
如何将每个匹配的结果转换为数组?有可能还是应该改变我的方法?
更新:
由于当前尝试的结果是一个很大的字符串,因此我尝试查找其他选项.弹出此窗口:
var array = Array.from(main.matchAll(itemListRegex),m = >> m [1]);
来源:
如果您想使用 matchAll
,例如 Array.from(mainbody.matchAll(itemListRegex),m => m[1])
,该修改如何?
在这种情况下,/(.* [^:])[\ r \ n] +(\ d *)[\ r \ n] +([$] \ d * \.\ d *)[\ r \ n]/g
用作正则表达式.
修改后的脚本:
const itemListRegex =/(.*[^:])[\r\n]+(\d*)[\r\n]+([$]\d*\.\d*)[\ r \ n]/g;var array = Array.from(mainbody.matchAll(itemListRegex),([,b,c,d])=> [b,Number(c),d]);
结果:
[[巧克力",1,"$ 8.58"],["Apples",2,"$ 40.40"],[香蕉",1,"$ 95.99"],["Candy",1,"$ 4.99"]]
- 结果与 TheMaster的答案相同.
脚本测试:
const mainbody =`产品数量价格巧克力1个$ 8.58苹果2个$ 40.40香蕉1个$ 95.99糖果1个$ 4.99小计:$ 149.96`;const itemListRegex =/(.*[^:])[\r\n]+(\d*)[\r\n]+([$]\d*\.\d*)[\r\n]/G;var array = Array.from(mainbody.matchAll(itemListRegex),([,b,c,d])=> [b,Number(c),d]);console.log(array)
注意:
- 关于
我如何以一种新的方式添加新行,使其每一行对应于找到的每个匹配项,并且每一列对应于每个属性?
,这意味着将这些值放入Spreadsheet?如果是这样,可以提供您期望的样本结果吗?
参考文献:
I'm trying to extract information from Gmail into Google Spreadsheet. The information in the email has a table structure with the following columns List of Products, QTY Sold and the Subtotal for each product. These repeat N times.
When accesing the information using message.getPlainBody()
I get the following text:
Product
Quantity
Price
Chocolate
1
$8.58
Apples
2
$40.40
Bananas
1
$95.99
Candy
1
$4.99
Subtotal:
$149.96
Progress
First I tried to use a regular expression to identify each row with all his elements:
- Product name: Any amount of characters that don't include ':' (.*)[^:]
- QTY Sold: Any number \d*
- Anything that looks like a SubTotal [$]\d*.\d*
Wrapping everything up it looks like this
function ExtractDetail(message){
var mainbody = message.getPlainBody();
//RegEx
var itemListRegex = new RegExp(/(.*)[^:][\r\n]+(\d*[\r\n]+[$](\d*\.\d*)[\r\n]+/g);
var itemList = mainbody.match(itemListRegex);
Logger.log(itemList);
}
And so far it works:
itemList: Chocolate 1 $8.58 ,Apples 2 $40.40 ,Bananas 1 $95.99 ,Candy 1 $4.99
However, I'm getting the following result:
- [Chocolate 1 $8.58]
- [Apples 2 $40.40]
- [Bananas 1 $95.99]
- [Candy 1 $4.99]
Instead of:
- [Chocolate] [ 1 ] [$8.58]
- [Apples] [ 2 ] [$40.40]
- [Bananas] [ 1 ] [$95.99]
- [Candy] [ 1 ] [$4.99]
Question
My question is, how can I append a new row in a way that it each row corresponds to each match found and that each column corresponds to each property?
How do I turn the result of each match into an array? Is it possible or should I change my approach?
Update:
Since the result of my current attemp is a large string I'm trying to find other options. This one poped up:
var array = Array.from(mainbody.matchAll(itemListRegex), m => m[1]);
Source: How do you access the matched groups in a JavaScript regular expression?
I'm still working on it. I still need to find how to add more columns and for some reason it starts on 'Apples' (following the examples), leaving 'Chocolates' behind.
Log:
Logger.log('array: ' + array);
If you want to use matchAll
like Array.from(mainbody.matchAll(itemListRegex), m => m[1])
, how about this modification?
In this case, /(.*[^:])[\r\n]+(\d*)[\r\n]+([$]\d*\.\d*)[\r\n]/g
is used as the regex.
Modified script:
const itemListRegex = /(.*[^:])[\r\n]+(\d*)[\r\n]+([$]\d*\.\d*)[\r\n]/g;
var array = Array.from(mainbody.matchAll(itemListRegex), ([,b,c,d]) => [b,Number(c),d]);
Result:
[
["Chocolate",1,"$8.58"],
["Apples",2,"$40.40"],
["Bananas",1,"$95.99"],
["Candy",1,"$4.99"]
]
- The result is the same with TheMaster's answer.
Test of script:
const mainbody = `
Product
Quantity
Price
Chocolate
1
$8.58
Apples
2
$40.40
Bananas
1
$95.99
Candy
1
$4.99
Subtotal:
$149.96
`;
const itemListRegex = /(.*[^:])[\r\n]+(\d*)[\r\n]+([$]\d*\.\d*)[\r\n]/g;
var array = Array.from(mainbody.matchAll(itemListRegex), ([,b,c,d]) => [b,Number(c),d]);
console.log(array)
Note:
- About
how can I append a new row in a way that it each row corresponds to each match found and that each column corresponds to each property?
, this means for putting the values to Spreadsheet? If it's so, can you provide a sample result you expect?
References:
这篇关于Google脚本:将RegEx匹配为2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!