而正则表达式匹配做一些事情

时间:2015-10-16 10:15:39

标签: python regex

我想解析一个文件,当模式匹配继续测试正则表达式时。因为我想处理返回值,我写道:

while (m=test.match(data)) != None:
    pass

在我的想法中,我想影响mtest.match(data),并且在测试后是否为无。但是我用这个语法:

  

SyntaxError:语法无效

如何在没有SyntaxError的情况下编写此内容?

2 个答案:

答案 0 :(得分:1)

你不能在循环子句中使用赋值:

m = test.match(data)
if not m:
    pass

答案 1 :(得分:1)

  

您不能在任何需要条件的语言中使用while循环进行分配。

我怀疑您正在尝试扫描文件以匹配模式,然后再使用它们。在这种情况下,match()不是正确的功能。 IMHO

variable = "123abc"
t = re.match("[a-z]+",variable) //returns null as word not in the beginning
t = re.search("[a-z]+",variable) // matches as it scans through
相关问题