Regex capitalize first letter every word, also after a special character like a dash

后端 未结 6 913
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 08:21

I use this #(\\s|^)([a-z0-9-_]+)#i for capitalize every first letter every word, i want it also to capitalize the letter if it\'s after a special mark like a da

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 08:48

    Here's my Python solution

    >>> import re
    >>> the_string = 'this is a test for stack-overflow'
    >>> re.sub(r'(((?<=\s)|^|-)[a-z])', lambda x: x.group().upper(), the_string)
    'This Is A Test For Stack-Overflow'
    

    read about the "positive lookbehind" here: https://www.regular-expressions.info/lookaround.html

提交回复
热议问题