Python: how to substitute and know whether it matched

前端 未结 3 461
温柔的废话
温柔的废话 2021-01-05 00:53

I know that re.sub(pattern, repl,text) can substitute when pattern matches, and then return the substitute.

My code is:

text = re.sub(p         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 01:29

    "Whether string contains numbers":

    for text1 in ('abc123def', 'adsafasdfafdsafqw', 'fsadfoi81we'):
        print("Text %s %s numbers." %
              ((text1, )  + (
                  ('does not contain',) if not any(c.isdigit() for c in text1)
                   else ('contains',))
               ))
    

提交回复
热议问题