2015-10-07 26 views
0

我需要找出一個給定的字符串是否是這種格式。正則表達式在python中爲特殊字符

anystr3ing1 : somesrritn3g 

下面是我的代碼

prog = re.compile("([a-zA-Z0-9]\D)" + ":" + ([a-zA-Z0-9]\D)") 

with open('data.txt','ru') as openfileobject: 
    for line in openfileobject: 
    if prog.match(line): 
     print line 

但其沒有給予任何輸出

+0

你的數據是什麼? – LetzerWille

+2

您有語法錯誤。這是你的實際代碼中的情況嗎?看第一行,在行尾附近缺少「+」和「([a-zA-Z0-9] \ D)」)'之間的「。 – idjaw

+0

此外,這個網站將有助於快速測試正則表達式:http://pythex.org – idjaw

回答

3

變化的正則表達式:

[a-zA-Z0-9]+\s+:\s+[a-zA-Z0-9]+ 

你的問題是,你只匹配從一個字符類別[a-zA-Z0-9],後面跟着一個非數字字符,然後是「:」,然後是[a-zA-Z0-9]