Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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_Nonetype - Fatal编程技术网

在使用python的正则表达式中找不到任何值

在使用python的正则表达式中找不到任何值,python,regex,nonetype,Python,Regex,Nonetype,我试图找到以#number 使用Python 3中的正则表达式将其转换为文件。 文件类似于: #123= toto(ikpm,guio,gyio) #126= tutu(kop,cfyg,jipo) #246= toto(gyui,rtf,kjoip) ... 和python代码: LineRe = re.compile('^#([0-9]+)= .+$') with open(path,'r') as f: for line in f: if "toto" in li

我试图找到以
#number
使用Python 3中的正则表达式将其转换为文件。 文件类似于:

#123= toto(ikpm,guio,gyio)
#126= tutu(kop,cfyg,jipo)
#246= toto(gyui,rtf,kjoip)
...
和python代码:

LineRe = re.compile('^#([0-9]+)= .+$')
with open(path,'r') as f:
    for line in f:
        if "toto" in line:
            lre = LineRe.fullmatch(line)
            print(lre)
            if not lre is None:
                number = lre.group(1)
                print(lre)
                print(number)
我的正则表达式
^#([0-9]+)=.+$
似乎可以,但我的代码总是打印“无”。。。
有什么问题吗?

我不知道
fullmatch()
所以当我尝试
search()
match()
时,效果很好

LineRe = re.compile('^#([0-9]+)= .+$')
with open(path,'r') as f:
    for line in f:
        if "toto" in line:
            lre = LineRe.search(line)
            print(lre)
            if not lre is None:
                number = lre.group(1)
                print(lre)
                print(number)
您还可以使用
match()

进口稀土

LineRe = re.compile('#([0-9]+)= .+')
with open(path,'r') as f:
    for line in f:
        if "toto" in line:
            lre = LineRe.match(line)
            print(lre)
            if not lre is None:
                number = lre.group(1)
                print(lre)
                print(number)
输出::

<_sre.SRE_Match object; span=(0, 26), match='#123= toto(ikpm,guio,gyio)'>
<_sre.SRE_Match object; span=(0, 26), match='#123= toto(ikpm,guio,gyio)'>
123
<_sre.SRE_Match object; span=(0, 26), match='#246= toto(gyui,rtf,kjoip)'>
<_sre.SRE_Match object; span=(0, 26), match='#246= toto(gyui,rtf,kjoip)'>
246

你只是忘了在运行fullmatch之前重新绕线。这方面的暗示在seer.The的评论中。他/她说,这对他们来说效果很好,因此他们显然只使用了一行文本或windows机器

lre = LineRe.fullmatch(line.rstrip())

您的代码在其他方面是完美的。

在您预期的匹配中是否有
toto
子字符串?您可能在regexr中使用
^#([0-9]+)=.+$
进行测试,并且您没有检查模式中的
toto
。请显示文件内容。您的正则表达式在我的测试中运行良好。@WiktorStribiżew:文件已添加到edit上谢谢,没关系,我喜欢这个网站:)
lre = LineRe.fullmatch(line.rstrip())