2015-10-16 33 views
2

我想分析一个文件,当一个模式匹配继续测试正则表达式。因为我想在返回值来运作,我写道:虽然正则表达式匹配做某事

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

在我的想法,我想,如果是无或不影响mtest.match(data)和测试之后。但有这样的语法,我有:

SyntaxError: invalid syntax

我怎样才能写这没有SyntaxError?

+0

我怀疑你正在使用这个任务错误的功能。使用'search()'而不是'match()' – Identity1

回答

1

不能在循环子句中使用分配:

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

You can't do assignments in a while loop in any language where it expects a condition.

我怀疑你正试图通过你的文件进行扫描,以匹配模式,然后在以后使用它们。在这种情况下,match()不是正确的功能。恕我直言

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