作者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://www.ptt.cc/bbs/Python/M.1518335796.A.E1C.html
→ djshen: 你有看過官方re文件裡的. +這些符號的意思嗎 02/11 15:59
推 sean50301: 這個問題感覺不用用到regex吧 02/11 16:54
→ vi000246: 你的regex要重學 02/11 18:02
→ liu02112: 我沒完整學過regex 但是謝謝樓上的連結 很有用 02/11 19:57
→ NTUGG: if (x.startswith('a') or x.endswith('6')) 02/11 20:40
→ NTUGG: 啊..or要換成and 02/11 20:42
→ NTUGG: 如果真的要用re.. 先re.compile(pattern)吧 02/11 20:46
推 Kazimir: '^a.*6$' 就是開頭a中間隨便6結尾 應該沒有and吧 02/11 22:32
推 largesperm: new_list = [x for x in old_list if x[0] == "a" or 02/12 07:40
→ largesperm: x[-1] == "6"] 02/12 07:41
→ WunoW: 這不是python的問題吧,是regexp的問題,有regexp板 02/12 10:22