从regEx中推送每个新匹配的数组

时间:2017-04-08 20:58:47

标签: arrays regex google-apps-script

我尝试使用以下代码解析一些xml文件:

   function parseXml() {
      var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml';
      var xml = UrlFetchApp.fetch(url).getContentText();

      var parseregexp = new RegExp (/.*em><no>(\d+)<\/no><title>(.+?)<\/title><price vat="\w+">(\d+.\d+|\d+)<\/price><unit>(.+?)<\/unit><free>(\d+)<\/free>(.|\s)*?<it/g)
      var parsedData = '$1 $2 $3 $4 $5 '
      var rangeRegex = [];

      var Pdata = xml.replace(parseregexp,parsedData)

      var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
      ss.getRange(1, 1).setValue(Pdata)
      }

这是xml片段:

<item><no>48514</no><title>The workpiece is the rod d 8x150mm P6AM5 HRC 64-66" CNIC"</title><price vat="yes">154.58</price><unit>Pcs</unit><free>59</free><remarks>Used to make an axial tool.
Hardness HRC64-66</remarks><img thumbnail="http://www.inpo.ru/index/I:48528/THUMBNAIL:0.jpg">http://www.inpo.ru/index/I: 48528 / PREVIEW: 0.jpg</img></item><item><no>48515</no><title>The workpiece is the rod d 8x200mm P6AM5 HRC 64-66"CNIC"</title><price vat="Yes">198.24</price><unit>pcs</unit><free>32</free><remarks>Used to make an axial tool.
Hardness HRC64-66</remarks><img thumbnail="http://www.inpo.ru/index/I:48528/THUMBNAIL:0.jpg">http://www.inpo.ru/index/I: 48528 / PREVIEW: 0.jpg</img></item>

结果Pdata是:

48514 The workpiece is the rod d 8x150mm P6AM5 HRC 64-66" CNIC" 154.58 Pcs 59 48515 The workpiece is the rod d 8x200mm P6AM5 HRC 64-66"CNIC" 198.24 pcs 32

在Pdata的这个例子中,我有来自所有正则表达式匹配的1个长字符串。如何从所有匹配中创建包含5列的数组?我想用&#34;来推动每场比赛到阵列#34;循环,但不知道它是如何制造的。对于任何帮助都会很满意

1 个答案:

答案 0 :(得分:1)

替代解决方案: 由于您尝试访问xml数据,因此可以使用XMLservice.parser

然而,在fetch调用中似乎存在问题,我无法获取整个数据(fetch提供截断文件,可能是超时,16mb文件),所以我下载了数据文件并将其上传到谷歌硬盘。 此文件可用于解析XML数据,如下所示:

function parseXml() {

      var file = DriveApp.getFileById("Xml File ID")   //Get the id of the uploaded file and replace it for "Xml File ID"
      var xml = file.getBlob().getDataAsString()
      // The below code gave a error for XML parser
     /*var url = 'http://www.inpo.ru/documents/pricelists/pricelist.xml';
       var options = {
       'method' : 'get',
       'contentType': 'application/xml',
       }
      var xml = UrlFetchApp.fetch(url,options).getBlob().getDataAsString()
      Logger.log(xml)*/
     // End of code with gave an error

 var arrayItems = []
 var XmlElem = ["no","title","price","unit","free"]       //Elements to look for
 var document = XmlService.parse(xml);
 var RCounter = 0
 var groups = document.getRootElement().getChildren();   //GetGroup Element
    for(var k = 0; k< groups.length; k++){               // Loop through each group element
      var main = groups[k].getChildren()                // Get sub groups in each group
  for (var j=0 ; j < main.length; j++){                 // Loop through each subGroups

   var mainChilds = main[j].getChildren()               //Get items in each subGroups
   for (var l = 0 ; l < mainChilds.length; l++){        // Loop through each items
   var items = mainChilds[l].getChildren();             // Get  elemetns like "no","title","price","units","free" in each item 
     arrayItems[RCounter] = []
   var total = 0;
    for (var i = 0; i < items.length; i++) {
     // Logger.log(items[i].getName())
      var index = XmlElem.indexOf(items[i].getName())       //Look for items and place the value at corresponding index
      if(index != -1)
        arrayItems[RCounter][index] = items[i].getValue()
     }  //End Loope for elements
     if(arrayItems[RCounter].length > 0)                   //in case the array is empty, reuse it
      RCounter++
    }   // End loop for items
    }   // End loop for sub Groups
    }   // End loop for Groups
  Logger.log(arrayItems) 
}

希望有所帮助!