Python String replace based on chars NOT in RegEx

前端 未结 3 1768
日久生厌
日久生厌 2021-02-18 22:41

Is it possible to create a reqex that finds characters that are NOT is a specific set?

Rather than Blacklisting a bunch of characters and replacing them, it would be eas

相关标签:
3条回答
  • 2021-02-18 22:54

    Try with:

    re.sub(r'[^a-zA-Z0-9]', "_", filename)
    
    0 讨论(0)
  • 2021-02-18 23:10

    Yes, use the ^ negation "modifier": r'[^.a-zA-Z0-9]'

    0 讨论(0)
  • 2021-02-18 23:17
    clean_filename = re.sub(r'[^.a-zA-Z0-9]', "_", filename)
    
    0 讨论(0)
提交回复
热议问题