Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式提取数据,并在Python2.7中对其进行计数_Regex_Python 2.7_Count_Expression - Fatal编程技术网

Regex 使用正则表达式提取数据,并在Python2.7中对其进行计数

Regex 使用正则表达式提取数据,并在Python2.7中对其进行计数,regex,python-2.7,count,expression,Regex,Python 2.7,Count,Expression,我需要使用正则表达式从文本中提取“Author”一词,并对其进行计数。这是我代码的一部分: counts=0 for line in handle: line=line.rstrip() counts=counts+1 x=re.findall('(^Author)', line) if len(myword)> 0: print counts,x print counts, "lines that matches Author" 我可以打开文件,

我需要使用正则表达式从文本中提取“Author”一词,并对其进行计数。这是我代码的一部分:

counts=0
for line in handle:
    line=line.rstrip()
    counts=counts+1
    x=re.findall('(^Author)', line)  
    if len(myword)> 0: print  counts,x
print  counts, "lines that matches Author"
我可以打开文件,找到单词,毫无问题地得到职位,但我不知道如何计算它们。有人能帮帮我吗?
谢谢。

为什么是正则表达式?因为我想学习如何掌握正则表达式。请使用另一个变量
totals=0
(靠近
count=0
行)。然后,在循环中放入
total+=len(x)
Why regex?因为我想学习如何掌握正则表达式。请使用另一个变量
totals=0
(靠近
count=0
行)。然后,在循环中放入
total+=len(x)