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
Python 我无法让我的代码计数并重置为零_Python_Regex - Fatal编程技术网

Python 我无法让我的代码计数并重置为零

Python 我无法让我的代码计数并重置为零,python,regex,Python,Regex,我在创建代码时遇到了一个问题,该代码将在模式不匹配时计数,并在该模式使用正则表达式匹配后重置回0。我将列出一个文件示例,并尝试演示我希望做的事情。无论文件中有多少匹配项,我都会收到-1。我也不确定文本文件中的数字应该如何格式化 regEx56“定位所有包含56的数字,无论位置如何)256 526 065。 regEx57“与56相同,但改用57” 这是我的密码: import re my_p3text = open("p3text.txt", "r") p3data = my_p3text.r

我在创建代码时遇到了一个问题,该代码将在模式不匹配时计数,并在该模式使用正则表达式匹配后重置回0。我将列出一个文件示例,并尝试演示我希望做的事情。无论文件中有多少匹配项,我都会收到-1。我也不确定文本文件中的数字应该如何格式化

regEx56“定位所有包含56的数字,无论位置如何)256 526 065。 regEx57“与56相同,但改用57”

这是我的密码:

import re

my_p3text = open("p3text.txt", "r")
p3data = my_p3text.read()


regEx56 = str(re.findall(r'\b[0-4]*(?:5[0-4]?6|6[0-4]?5)[0-4]*\b',p3data))
regEx57 =str(r'\b[0-4]*(?:5[0-4]?7|7[0-4]?5)[0-4]*\b',p3data)



for line in my_p3text:
    match=regEx56.search(line)
    count = 0
    print (match)


else:
    count = -1
print (count)
首先:

for line in my_p3text:
    match=regEx56.search(line)
    count = 0
    print (match)


else:
    count = -1
这意味着当没有更多的行时,它将打印-1,这始终是。在for循环中放入if-else语句

此外,现在你的数字只能以0-4、5、6或7开头;在6-7和5之间可能有0-4(反之亦然)。它的结尾方式也与开头类似

如果这是你想要的,那没关系,否则试试

(\d*((5\d*?[67])|([67]\d*?5))\d*)

模式到底是什么。你能举个例子吗?所以数字列表是:107 328 156 725。我想用56和75跟踪数字,正则表达式捕捉它们。但是计数不起作用。r'\b[0-4]*(?:5[0-4]?6|6[0-4]?5[0-4]*\b'=3位数字中的56和'\b[0-4]*(?:5[0-4]?7[0-4]?5)[0-4]*\b'对57也一样
(\d*((5\d*?[67])|([67]\d*?5))\d*)