输入字符串,输出-包含2个字符串的列表:
一些示例:
str1 = dir/?
str2 = dir c:\users /A/B
str3 = dir>1.txt
list1 = ['dir', '/?']
list2 = ['dir', ' c:\users /A/B'] #with space
list3 = ['dir', '>1.txt']
r"([a-zA-Z_]+)(.*)"
应该这样做,尽管我对#4中的下划线感到困惑,因为您在标题中写了“任何字母”。
在代码中实际使用它:
import re
str1 = "dir/?"
m = re.match(r"([a-zA-Z_]+)(.*)", str1)
list1 = list(m.groups()) #list1 is now ['dir', '/?'], as desired
如果您实际上不需要下划线,则只需将其从第一个字符类中删除: r"([a-zA-Z]+)(.*)"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句