簡體   English   中英

如何將 Perl 正則表達式轉換成 Python?

[英]How to convert Perl regular expression into 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

該表達式將作為在Python。 只需使用re.VERBOSE開關或刪除所有空格。

演示:

>>> 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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM