我正试图从行中捕获数字10.0006:
总时间:10.0006s
是文本文件中的一行。到目前为止,我有:
var doc = DocumentApp.openByUrl('removed url').getBody().editAsText();
//locate totalTime text
var regEx = "/(?<=total time: )([\d\.]+[\d])(?=s)/";
var totalTime = doc.findText(regEx).getElement().getText();
//display values in log
Logger.log(totalTime);
根据https://developers.google.com/apps-script/reference/document/text#findText(String),findText不支持捕获组,是否还有其他方法可以捕获模式?提前谢谢!
答案 0 :(得分:1)
这就是我最终的目标,感谢@Kos的帮助!
// locate totalTime text
var findLine = "total time:";
var foundRange = doc.findText(findLine);
// if range is found
if (foundRange)
{
var regEx = /[\d\.]+[\d](?=s)/
// get related element and it's text
var foundText = foundRange.getElement().getText();
Logger.log(foundText);
// capture needed group of characters
var totalTime = foundText.match(regEx);
//display values in log
Logger.log(totalTime);
}
答案 1 :(得分:0)
您可以尝试执行两个步骤
findText
所需的元素。 match
来匹配所需的字符组。var doc = DocumentApp.openByUrl('removed url').getBody().editAsText();
// locate totalTime text
var regEx = "total time: ([0-9.]+)s";
var foundRange = doc.findText(regEx);
// if range is found
if (foundRange)
{
// get related element ant it's text
var foundText = foundRange.getElement().getText();
// capture needed group of characters
var totalTime = foundText.match(regEx)[1];
//display values in log
Logger.log(totalTime);
}