Python: Regular expression to match alpha-numeric not working?

前端 未结 3 1721
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 10:15

I am looking to match a string that is inputted from a website to check if is alpha-numeric and possibly contains an underscore. My code:

if re.match(\'[a-zA         


        
3条回答
  •  情歌与酒
    2021-01-01 10:44

    Python has a special sequence \w for matching alphanumeric and underscore when the LOCALE and UNICODE flags are not specified. So you can modify your pattern as,

    pattern = '^\w+$'

提交回复
热议问题