看板 RegExp 關於我們 聯絡資訊
各位好 , 小弟剛開始使用python練習regex 請教一下 , 下面我想印出['1987b'] , 試了一陣子不曉得re哪個地方需要調整= =||| 感謝提點! r = re.compile('[\d]{4,6}[a-z]+[^\/]+[\w]*') string_list = ['123', '1911a/1/1', '1987b'] list_new = list(filter(r.match, string_list)) print(list_new) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.163.120.94 ※ 文章網址: https://www.ptt.cc/bbs/RegExp/M.1436779523.A.9CD.html
mars90226: 看你想怎麼做,你需要1987b後面就結束的話 07/13 21:00
mars90226: 用'\d{4,6}[a-z]+$',如果你是不想要後面有'/'的話 07/13 21:02
mars90226: 用'\d{4,6}[a-z]+(?!\/)' 07/13 21:02
Czero: 感謝您, 但為何我用[^\/]這樣方式不行呢? 07/13 23:18
mars90226: 因為這樣會要求後面不能是結尾,你這個例子就會出錯 07/14 01:12