简体   繁体   中英

How to convert Perl regular expression into Python?

How do you convert this Perl regular expression into Python:

(?:

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

)

|

n't

That expression will work as is in Python. Just use the re.VERBOSE switch or remove all the whitespace.

Demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM