Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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中的正则表达式测试很好,但没有正确实现_Python_Python 3.x - Fatal编程技术网

python中的正则表达式测试很好,但没有正确实现

python中的正则表达式测试很好,但没有正确实现,python,python-3.x,Python,Python 3.x,我正在做一个个人项目,这是我第一次使用RegEx,所以如果我误解了文档,请原谅我。在我的程序中,我正在阅读一个网页并试图解析其中的信息。我已经在pythex上测试了我的表达式,它准确地突出显示了我感兴趣的字符串部分,但是当我在命令行中使用相同的输入测试代码时,我得到了一个奇怪的输出 我尝试过使用搜索、findall和match来处理各种奇怪的输出,但没有成功 def getStats(): playername=input(“输入您的OSRS名称:”) 尝试: 使用urllib.request.

我正在做一个个人项目,这是我第一次使用RegEx,所以如果我误解了文档,请原谅我。在我的程序中,我正在阅读一个网页并试图解析其中的信息。我已经在pythex上测试了我的表达式,它准确地突出显示了我感兴趣的字符串部分,但是当我在命令行中使用相同的输入测试代码时,我得到了一个奇怪的输出

我尝试过使用搜索、findall和match来处理各种奇怪的输出,但没有成功

def getStats():
playername=input(“输入您的OSRS名称:”)
尝试:
使用urllib.request.urlopen(“https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=“+playername)作为回应:
page=response.read()
page=str(第页)
levels=re.findall(r'[,\d\d,]',第页)
打印(级别)
这是我想解析网页中的数字并将其存储在列表中的代码部分。如果您查看我的pythex链接,输出应该是

Output: ['77', '80', '76', '91', ...
相反,我得到的输出是

Output: ['2', '9', '1', '2', '2', ...

我发布的代码有什么明显的问题吗?我假设我用python编写表达式的方式有问题。

表达式中不需要方括号——它们用于字符类,表示“仅其中一个字符”。您的
[,\d\d,]
表达式实际上表示“逗号或数字或数字或逗号”

您需要的是用于捕获组的括号:
r',(\d\d),“

关于Regex101的示例:

有关详细信息:

请尝试以下方法:

re.findall('(?<=,)\d{2}(?=,)', page)
下面是一个关于如何在Python中实现这一点以及如何仅获取数字(而不是逗号)的示例

重新导入
regex=r“,(\d\d),”
测试_str="291230,1619,43801314 426005,77,1526319 324883,80,2064954 669155,76,1440237 320402,91,5933387 144963,99,13040711 380507,70,743124 387592,84,3119589 151260,91,6150297 703369,64,413056 599216,63,393779 195668,80,2096232 191977,82,2540831 326908,70,748218 709559,51,113909 405685,67,547977 572221,48,83492 443568,65,456357 325912,60,273874 436817,70,789871 364612,58,232633 314387,50,106976 184820,71,853614 378565,52,131877 -1,-1 -1,-1 -1,-1 401271,17 -1,-1 218157,8 610933,1 319030,7 247208,1 -1,-1"
matches=re.finditer(regex、test\u str、re.MULTILINE)
对于matchNum,在枚举中匹配(匹配,开始=1):
打印(“在{start}-{end}:{Match}找到了Match{matchNum}”。格式(matchNum=matchNum,start=Match.start(),end=Match.end(),Match=Match.group())
对于范围(0,len(match.groups())中的groupNum:
groupNum=groupNum+1
打印(“在{start}-{end}:{Group}找到的组{groupNum}”。格式(groupNum=groupNum,start=match.start(groupNum),end=match.end(groupNum),Group=match.Group(groupNum)))

最好提供一个示例播放器名称。返回my pythex链接中使用的数据的播放器名称是'arayvenn',您已将\d指定为字符类。相反,您应该将其指定为模式。类似于r“\d{2}”。没有用于阅读和理解文档的子文件。
['77', '80', '76', '91', '99', '70', '84', '91', '64', '63', '80', '82', '70', '51', '67', '48', '65', '60', '70', '58', '50', '71', '52']