2015-10-05 38 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' 
相关问题