看板 RegExp 關於我們 聯絡資訊
大家好 我是python的新手 最近遇到一些問題想請教高手們 我想將類似 2000.10.10|4.5|中文|5566 分成四塊 用group(0-3)取出來 不過試了很多方法一直卡住 想請大家幫忙看看感謝~~ 我的code: import re a= "2000.10.10|4.5|中文|5566" m=re.match(r"(^.{10})|(.{3})|(.{2})|(\d{4}$)",a) print m.group(0) print m.group(1) print m.group(2) print m.group(3) 輸出結果: 2000.10.10 2000.10.10 None None -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.121.249.196 ※ 文章網址: https://www.ptt.cc/bbs/RegExp/M.1466842114.A.83A.html ※ 編輯: king4647 (203.121.249.196), 06/25/2016 16:10:21
s25g5d4: | 是 or 的意思 要 match 字面上的 | 需要轉譯 06/25 17:38
s25g5d4: 改成 \| 就行了 06/25 17:39
感謝大大 解出來了 不過想問問觀念@@~ a= "2000.10.10|4.5|中文|5566" m=re.match(r"(^.+)\|(.+)\|(.+)\|(\d{4}$)",a) print m.group(0) print m.group(1) print m.group(2) print m.group(3) print m.group(4) 輸出: 2000.10.10|4.5|中文|5566 2000.10.10 4.5 中文 5566 想問為什麼m.group(0)會是全部呢? 謝謝 ※ 編輯: king4647 (203.121.249.196), 06/25/2016 18:44:20
LPH66: 基本上規定就是 0 是全部, 1 是第一個, 2 是第二個 etc 06/25 19:24
LPH66: 許多語言中使用 regexp 做比對的結果都是這樣定的 06/25 19:25
king4647: 感謝~~~ 06/25 23:16