[问题] 新手对Python String的问题

楼主: liu02112 (好想吃惭愧棒棒糖.......)   2018-02-11 15:56:30
各位好
最近小弟练习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)]
全部失败......请问有人知道方法吗??
谢谢各位
作者: djshen (djshen)   2018-02-11 15:59:00
你有看过官方re文件里的. +这些符号的意思吗
作者: sean50301 ( (づ′・ω・)づ)   2018-02-11 16:54:00
这个问题感觉不用用到regex吧
作者: vi000246 (Vi)   2018-02-11 18:02:00
你的regex要重学
作者: alex0914 (Life is short)   2018-02-11 18:41:00
https://regexr.com/ 先看一下为啥不 match
楼主: liu02112 (好想吃惭愧棒棒糖.......)   2018-02-11 19:57:00
我没完整学过regex 但是谢谢楼上的连结 很有用
作者: NTUGG (GG)   2018-02-11 20:40:00
if (x.startswith('a') or x.endswith('6'))啊..or要换成and如果真的要用re.. 先re.compile(pattern)吧
作者: Kazimir (Kazimir)   2018-02-11 22:32:00
'^a.*6$' 就是开头a中间随便6结尾 应该没有and吧
作者: largesperm (reindeer)   2018-02-12 07:40:00
new_list = [x for x in old_list if x[0] == "a" orx[-1] == "6"]
作者: WunoW (WunoW)   2018-02-12 10:22:00
这不是python的问题吧,是regexp的问题,有regexp板
作者: galeondx   2018-03-06 04:44:00

Links booklink

Contact Us: admin [ a t ] ucptt.com