Upgrade to Pro — share decks privately, control downloads, hide ads and more …

正規表現

 正規表現

2017/03/01のB3ゼミで発表

Yumeto Inaoka

March 01, 2017
Tweet

More Decks by Yumeto Inaoka

Other Decks in Technology

Transcript

  1. ਖ਼نදݱΛ࢖Θͳ͍৔߹ ˗ 8JOEPXT΍8JOEPXTΛݕࡧ os = ["Windows 95", "Windows95", "windows 95",

    “windows95”, "Windows 98", "Windows98",
 "windows 98", “windows98”]
 
 print("ok" if s in os else “ng")
 
 # Windows95 -> ok
 # windows 98 -> ok
 # Windows Me -> ng 
  2. ਖ਼نදݱΛ࢖ͬͨ৔߹ ˗ 8JOEPXT΍8JOEPXTΛݕࡧ os = r"[Ww]indows ?9[58]"
 
 print("ok" if

    re.match(os, s) else “ng")
 
 
 
 # Windows95 -> ok
 # windows 98 -> ok
 # Windows Me -> ng 
  3. ਖ਼نදݱΛ࢖ͬͨ৔߹ ˗ ܞଳి࿩൪߸Λݕࡧ phone = r"0[789]0-?\d{4}-?\d{4}"
 
 print("ok" if re.match(phone,

    s) else "ng")
 
 
 
 # 070-0123-4567 -> ok
 # 09009876543 -> ok
 # 06-6012-3456 -> ng 
  4. ਖ਼نදݱͷߏจ ˗ ೚ҙͷҰจࣈ ptn = r”r.c.”
 
 # rice ->

    ok
 # rich -> ok
 # rock -> ok
 # roc -> ng
 # reach -> ng 
  5. ਖ਼نදݱͷߏจ ˗ ಛఆͷҰจࣈ<ʜ> ptn = r”[bdm]ay”
 
 # bay ->

    ok
 # day -> ok
 # may -> ok
 # way -> ng
 # say -> ng
 # pay -> ng 
  6. ਖ਼نදݱͷߏจ ˗ จ಄? ptn = r”^re”
 
 # reborn ->

    ok
 # revenge -> ok
 # replay -> ok
 # preview -> ng
 # unstable -> ng
 # alive -> ng 
  7. ਖ਼نදݱͷߏจ ˗ จ຤ ptn = r”able$”
 
 # movable ->

    ok
 # readable -> ok
 # able -> ok
 # stably -> ng
 # assemble -> ng
 
  8. ਖ਼نදݱͷߏจ ˗ ճҎ্ͷ܁Γฦ͠  ptn = r”w*”
 
 # w

    -> ok
 # ww -> ok
 # wwww -> ok
 # -> ok
 # oh -> ng
 # yeah -> ng
 # hey -> ng 
  9. ਖ਼نදݱͷߏจ ˗ ճҎ্ͷ܁Γฦ͠  ptn = r”oh+”
 
 # oh

    -> ok
 # ohh -> ok
 # ohhhh -> ok
 # o -> ng
 # ok -> ng 
  10. ਖ਼نදݱͷߏจ ˗ ճ͔ճ  ptn = r”^b?l?ack”
 
 # ack

    -> ok
 # lack -> ok
 # back -> ok
 # black -> ok
 # block -> ng
 # slack -> ng 
  11. ਖ਼نදݱͷߏจ ˗ Nճ͔ΒOճ\N O^ ptn = r”^g{2,4}d$”
 
 # good

    -> ok
 # goood -> ok
 # gooood -> ok
 # gd -> ng
 # god -> ng
 # goooood -> ng