作者liu02112 (好想吃惭愧棒棒糖.......)
看板Python
标题[问题] 新手对Python String的问题
时间Sun Feb 11 15:56:30 2018
各位好
最近小弟练习string问题 Code如下所示:
old_list = ['abc123', 'aef456', 'ghi789']
new_list = [x for x in old_list if re.search('^a|6$', x)]
new_list
我能找出'a'开头或'6'结尾的数字(or |)
但是请问and要怎麽表示??我找都找不到 我尝试
new_list = [x for x in old_list if re.search('^ab & 6$', x)]
new_list = [x for x in old_list if re.search('^a+6$', x)]
new_list = [x for x in old_list if re.search('^a.6$', x)]
全部失败......请问有人知道方法吗??
谢谢各位
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 42.73.235.199
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1518335796.A.E1C.html
1F:→ djshen: 你有看过官方re文件里的. +这些符号的意思吗 02/11 15:59
2F:推 sean50301: 这个问题感觉不用用到regex吧 02/11 16:54
3F:→ vi000246: 你的regex要重学 02/11 18:02
5F:→ liu02112: 我没完整学过regex 但是谢谢楼上的连结 很有用 02/11 19:57
6F:→ NTUGG: if (x.startswith('a') or x.endswith('6')) 02/11 20:40
7F:→ NTUGG: 啊..or要换成and 02/11 20:42
8F:→ NTUGG: 如果真的要用re.. 先re.compile(pattern)吧 02/11 20:46
9F:推 Kazimir: '^a.*6$' 就是开头a中间随便6结尾 应该没有and吧 02/11 22:32
10F:推 largesperm: new_list = [x for x in old_list if x[0] == "a" or 02/12 07:40
11F:→ largesperm: x[-1] == "6"] 02/12 07:41
12F:→ WunoW: 这不是python的问题吧,是regexp的问题,有regexp板 02/12 10:22