Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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
python re.match()未返回预期结果_Python_Regex - Fatal编程技术网

python re.match()未返回预期结果

python re.match()未返回预期结果,python,regex,Python,Regex,我正在尝试使用re.match选择python字符串的一部分: revenue = "Revenue;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,190,140,1926,14,143;Coffee,325,19,293,1491,162;Water,682,14,852,56,659;Milk,829,140,609,120,87;;Expenses;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;

我正在尝试使用re.match选择python字符串的一部分:

revenue = "Revenue;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,190,140,1926,14,143;Coffee,325,19,293,1491,162;Water,682,14,852,56,659;Milk,829,140,609,120,87;;Expenses;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,120,65,890,54,430;Coffee,300,10,23,802,235;Water,50,299,1290,12,145;Milk,67,254,89,129,76;;"
revenue = re.match(r"(?<=Revenue;;).*(?=;E)", file_content)
print(revenue)
因此,我假设我的python实现有问题,但是,我在python正则表达式文档中找不到任何帮助我的信息。使用python 2和python 3进行了尝试


我可能做错了什么

使用
re.search

Ex:

import re

revenue = "Revenue;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,190,140,1926,14,143;Coffee,325,19,293,1491,162;Water,682,14,852,56,659;Milk,829,140,609,120,87;;Expenses;;Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,120,65,890,54,430;Coffee,300,10,23,802,235;Water,50,299,1290,12,145;Milk,67,254,89,129,76;;"
revenue = re.search(r"(?<=Revenue;;).*(?=;E)", revenue)
print(revenue.group())
Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,190,140,1926,14,143;Coffee,325,19,293,1491,162;Water,682,14,852,56,659;Milk,829,140,609,120,87;

根据Python文档

重新匹配(模式、字符串、标志=0)

如果字符串的开头有零个或多个字符与 正则表达式模式,返回相应的匹配对象。 如果字符串与模式不匹配,则返回None;请注意 与零长度匹配不同


因此,您可能应该使用
re.search
re.findall

re.match
从字符串的开头开始匹配,因此后面的查找
(?
Item,Johnver,Vanston,Danbree,Vansey,Mundyke;Tea,190,140,1926,14,143;Coffee,325,19,293,1491,162;Water,682,14,852,56,659;Milk,829,140,609,120,87;