条件匹配错误的模式匹配时返回的表达式(如果我没记错的话)

时间:2019-01-07 09:33:53

标签: regex perl

我是perl的新手。尝试使用V基本代码,在那里我可以看到错误的模式匹配正在恢复真实。 将代码粘贴到此处。

my @a =["string", 12]; if( $a[0]=~ /(\d+)/ ){  print"anything";}

我认为程序控件不应该输入print语句,但实际上应该输入。 不明白字符串如何与数字值(\ d +)匹配。 预先感谢。

1 个答案:

答案 0 :(得分:5)

您的数组包含一个元素,即数组引用 import scrapy import re import csv class VoteSpider(scrapy.Spider): name = 'VoteSpider' start_urls = ['https://www.wahlrecht.de/umfragen/forsa.htm'] def __init__(self): self.CDU = {} self.SPD = {} self.FDP = {} self.Green = {} self.left = {} def parse(self, response): regex = r"[forsa]+[\/]+[0-9]+.htm" tableBody = response.xpath('//*[@class="wilko"]//tbody') hxs = scrapy.Selector(response) all_links = hxs.xpath('*//a/@href').extract() yearLinks = [] for link in all_links: matches = re.search(regex, link, re.MULTILINE) if matches: yearLinks.append(link) for link in yearLinks: newlink = "https://www.wahlrecht.de/umfragen/"+ link yield scrapy.Request(url = newlink, callback=self.parseLink, meta={'name':link}) self.parseTable(tableBody) def parseTable(self,tableBody): CDU= [] SPD = [] FDP= [] Green= [] left= [] rows = tableBody.xpath('//tr') del rows[:5] for row in rows: CDU.append(row.xpath('td//text()')[2].extract()) SPD.append(row.xpath('td//text()')[3].extract()) Green.append(row.xpath('td//text()')[4].extract()) FDP.append(row.xpath('td//text()')[5].extract()) left.append(row.xpath('td//text()')[6].extract()) with open('CDU'+'Current'+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('SPD'+'Current'+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('left'+'Current'+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('Green'+'Current'+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('FDP'+'Current'+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) self.CDU['Current'] = [] self.SPD['Current'] = [] self.Green['Current'] = [] self.FDP['Current'] = [] self.left['Current'] = [] self.CDU['Current'].append(CDU) self.SPD['Current'].append(SPD) self.Green['Current'].append(Green) self.FDP['Current'].append(FDP) self.left['Current'].append(left) def parseLink(self, response): CDU= [] SPD = [] FDP= [] Green= [] left= [] name = response.meta.get('name') yearNumber = re.findall('\d+',name)[0] x = 0 if yearNumber == '2007': x = 4 elif yearNumber == '1998': x = 3 elif yearNumber == '1999': x = 3 elif yearNumber == '2000': x = 3 elif yearNumber == '2001': x = 3 elif yearNumber == '2002': x = 3 elif yearNumber == '2003': x = 3 elif yearNumber == '2004': x = 3 elif yearNumber == '2005': x = 5 elif yearNumber == '2006': x = 3 elif yearNumber == '2008': x = 4 elif yearNumber == '2013': x = 4 tableBody = response.xpath('//*[@class="wilko"]//tbody') rows = tableBody.xpath('//tr') del rows[:x] for row in rows: CDU.append(row.xpath('td//text()')[2].extract()) SPD.append(row.xpath('td//text()')[3].extract()) Green.append(row.xpath('td//text()')[4].extract()) # print(row.xpath('td//text()').extract()) FDP.append(row.xpath('td//text()')[5].extract()) left.append(row.xpath('td//text()')[6].extract()) with open('CDU'+yearNumber+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('SPD'+yearNumber+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('left'+yearNumber+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('Green'+yearNumber+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) with open('FDP'+yearNumber+'.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(CDU) self.CDU[yearNumber]= [] self.SPD[yearNumber] = [] self.Green[yearNumber] = [] self.FDP[yearNumber] = [] self.left[yearNumber] = [] self.CDU[yearNumber].append(CDU) self.SPD[yearNumber].append(SPD) self.Green[yearNumber].append(Green) self.FDP[yearNumber].append(FDP) self.left[yearNumber].append(left) 。当数组引用用作字符串时,它会字符串化为类似["string", 12]的数字。

要分配两个元素,请使用

ARRAY(0x1e80cb8)