2015-10-05 96 views
-8

怎樣把這個Perl的正則表達式成Python:如何將Perl正則表達式轉換爲Python?

(?: 

    ^(?:never|no|nothing|nowhere|noone|none|not| 
     havent|hasnt|hadnt|cant|couldnt|shouldnt| 
     wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint 
    )$ 

) 

| 

n't 
+1

轉到http://pythex.org並測試了正則表達式。這是找出你的正則表達式的特定語法是否有效的最快方法。 – idjaw

回答

4

該表達式將工作如在Python。只需使用re.VERBOSE switch或刪除所有空白。

演示:

>>> import re 
>>> pattern = re.compile('''\ 
... (?: 
... 
...  ^(?:never|no|nothing|nowhere|noone|none|not| 
...   havent|hasnt|hadnt|cant|couldnt|shouldnt| 
...   wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint 
... )$ 
... 
...) 
... 
... | 
... 
... n't 
... ''', flags=re.VERBOSE) 
>>> pattern.match('never').group() 
'never' 
>>> pattern.match('wouldnt').group() 
'wouldnt' 
相關問題