美文网首页
python字符串多个分割符(借助正则表达式re)

python字符串多个分割符(借助正则表达式re)

作者: 闫阿佳 | 来源:发表于2017-11-26 13:08 被阅读0次

    传统的字符串分割,只是支持一个分隔符

    >>> s = "Hello python-world|string,split?"
    >>> s.split('-')
     ['Hello python', 'world|string,split?']
    

    通过借助正则表达式,可以完成多个字符分割;

    >>> import re
    >>> re.split('[-|,?]', s)
    
     ['Hello python', 'world', 'string', 'split', '']
    
    

    相关文章

      网友评论

          本文标题:python字符串多个分割符(借助正则表达式re)

          本文链接:https://www.haomeiwen.com/subject/weykbxtx.html