Monica_MTF 2023-02-11 16:40 采纳率: 73.9%
浏览 37
已结题

Python#正则表达式#的问题接收字符串返回列表使用split()函数不能用-^

Python#434
函数main()接收一个字符串,要求返回其中所有单词组成的列表。
例如,main(r'one,two,three,four/five\six?seven[eight]nine|ten')
返回['one','two','three','four','five','six','seven','eight','nine','ten'];

main(r'one1two2three3four4five5six6seven7eight')
返回['one','two','three','four','five','six','seven','eight'];

main(r'one two three four,five.six.seven,eight,nine')
返回['one','two','three','four','five','six','seven','eight','nine',];

要求使用正则表达式模块的split()函数,但不能使用减号字符和尖号字符^
考点:正则表达式,split()函数

import re

def main(text):
     pass
  • 写回答

5条回答 默认 最新

  • cjh4312 2023-02-11 17:49
    关注
    
    def main(text):
        regEx=re.compile(r'\W|\d+')
        words=regEx.split(text)
        print(words)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 2月21日
  • 已采纳回答 2月13日
  • 修改了问题 2月11日
  • 创建了问题 2月11日