2015-10-16 27 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