[正規表現]メタ文字をはじめから丁寧に

8aebb410308ec2c655550798e92cd2df?s=47 ken3ypa
October 27, 2018
210

 [正規表現]メタ文字をはじめから丁寧に

Tama.rb 2018/10/27(土)発表

8aebb410308ec2c655550798e92cd2df?s=128

ken3ypa

October 27, 2018
Tweet

Transcript

  1. ϝλจࣈΛ͸͡Ί͔Βஸೡʹ ͚Μ(@alyoalyoalyo) 2018೥10݄27೔(౔) Tama.rb νΣϦʔຊྠಡձ

  2. ਖ਼نදݱɺ֮͑ͯ·͔͢ʁ ͋Δ೔ͷձ࿩

  3. Θ͔ͬͯεοΩϦ͔Βͷ๨ΕͯαούϦ

  4. ໨ඪͱ͢Δͱ͜Ζ • ਖ਼نදݱɺάάΔճ਺ΛݮΒ͍ͨ͠ • ɹຖճ͍͔ͪΒάάΔͷ͸େม • ɹ͔ͱ͍ͬͯશ෦͸͙֮͑͢ΒΕͳ͍ • ɹ࠷௿ݶ֮͑Δͱ͜Ζ͚͍֮ͩ͑ͨ

  5. ࠷௿ݶʝ [\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x 80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\ \x80-\xff\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000 -\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\ \\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\04 0\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\ xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80 -\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\0 15()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\x

    ff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t] *)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:" .\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xf f][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*( ?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\ \x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[ \040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x8 0-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\ x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\03 7\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\ \\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\x
  6. ͦ͏΍ɺϝλจࣈ΍

  7. ϝλจࣈͬͯʁ • ͦͷจࣈຊདྷͷҙຯͰ͸ͳ͘ɺϓϩάϥϜͰಛผͳҙ ຯΛ࣋ͨͤͨจࣈͷ͜ͱ • (ྫɿʮ+ʯͱϝλจࣈʮ+ʯ͸ผͷҙຯΛ࣋ͭ) • ௚༁͢Δͱ௒จࣈɹ • ਖ਼نදݱΛࢧ͑Δɺॏཁͳٕज़

    • ͦΜͳʹݸ਺ଟ͘ͳ͍(͚Ͳ͙͢๨ΕΔ) • ͱ͍͏͔͜Εԡ͑͞ͳ͍ͱ࿩͕࢝·Βͳ͍
  8. ͱ͍͏͜ͱͰ ࠷௿ݶ͓͖͍֮͑ͯͨ ϝλจࣈΛ͝঺հ

  9. ͰΜʂʂ

  10. ·ͣࠓճ֮͑Δ΂͖΋ͷΛશͯோΊΔ /P ϝλจࣈ  ?
     

       \^  c    <>  a
  11. ଟ͘Ͷ ( ^ω^;)

  12. ͋ͱ͜ͷه߸ͳΜͯಡΉΜͩΑ ^ ←͜Εͱ͔ []{}() ͜Εͱ͔͘͢͝ฆΒΘ͍͠

  13. ͷͰ

  14. άϧʔϐϯάԽͯ͠ ݸผʹղઆͯ͠Έͨ

  15. 3ͭͷάϧʔϓ

  16. ࡾͭͷάϧʔϓ • (1)1จࣈϚον • (2)܁Γฦ͠ • (3)άϧʔϐϯά͢Δ΋ͷ ɹɹɹɹɹɹɹɹɹɹɹɹͷࡾͭͰ͝঺հ͠·͢

  17. (1) 1จࣈϚον

  18. ҰͭͷจࣈྻΛѻ͏ ϝλจࣈ ػೳ ? ߦͷઌ಄ͷҐஔʹ Ϛον  ߦͷ຤ඌͷҐஔʹ Ϛον 

    ௚લͷจࣈʹϚον
  19. ߦ಄Ϛον ߲໨ આ໌ ه߸ ? ಡΈํ ΩϟϨοτɺϋοτ ػೳ ߦͷઌ಄ͷҐஔʹ Ϛον

    ༻ྫ IUUQSVCVMBSDPNS 4-"MK[RS#
  20. ߦ಄Ϛον • จষͷઌ಄ͷҐஔʹϚον • ٯʹݴ͑͹ઌ಄ʹͳ͍ͱϚον͠ͳ͍

  21. ߦ຤Ϛον ߲໨ આ໌ ه߸  ಡΈํ υϧɾυϧϚʔΫ ػೳ ߦͷ຤ඌͷҐஔʹ Ϛον

    ༻ྫ IUUQSVCVMBSDPNS J%*FS.
  22. ߦ຤Ϛον • ߦͷ຤ඌͷҐஔʹϚον • ٯʹݴ͑͹຤ඌʹͳ͍ͱϚον͠ͳ͍ • ͭ·Γ͖ͬ͞ͷٯ

  23. ͳΜͰ΋Ϛον ߲໨ આ໌ ه߸ ɾ ಡΈํ ϐϦΦυɺυοτ ػೳ ௚લͷจࣈʹϚον ༻ྫ

    IUUQSVCVMBSDPNS Z1F3KXOZ(
  24. ͳΜͰ΋Ϛον • ೚ҙͷ1จࣈʹϚονͤ͞Δ͜ͱ͕Ͱ͖Δ • ϙʔΧʔ΍େ෋߽ʹ͓͚ΔδϣʔΧʔʹ͍ۙ

  25. (2)܁Γฦ͠

  26. ܁Γฦ͠ ϝλจࣈ Ϛον͢Δέʔε ௚લͷจࣈ΍ύλʔϯ͕ ճҎ্࿈ଓ͢Δ ௚લͷจࣈ΍ύλʔϯ͕ ճҎ্࿈ଓ͢Δ ௚લͷจࣈ΍ύλʔϯ͕ ճ΋͘͠͸ճҎ্࿈ଓ͢Δ \O^

    ௚લͷจࣈ΍ύλʔϯ͕ OճҎ্࿈ଓ͢Δ
  27. ܁Γฦͬͯ͠ʁ • ܁Γฦ͠ͷ਺ྔΛࢦఆ͠ɺ֘౰͢Δ΋ͷͱϚονϯά • ʮྔࢦఆࢠʯͱ΋ݺ͹ΕΔ • ӳޠͰ͍͏ͱgeneral repetition quantifier(൚༻ͷ ܁Γ

    ฦ͠ͷ਺ྔΛද͢Ҽࢠ)
  28. * ߲໨ આ໌ ه߸ ಡΈํ ΞελϦεΫ ػೳ ௚લͷจࣈ΍ύλʔϯ͕ճҎ্ ࿈ଓ͢Δ΋ͷʹϚον ༻ྫ

    IUUQSVCVMBSDPNS OI94/-GV.
  29. * • 0ճҎ্Ϛονͬͯʁ • ௚લͷҰจࣈ͸লུՄೳɺͱ͍͏ҙຯ • ਤத͸ͦΕͧΕ0ճɾ1ճɾ2ճɾ3ճϚον

  30. ? ߲໨ આ໌ ه߸ ಡΈํ ΫΤενϣϯϚʔΫ ػೳ ௚લͷจࣈ΍ύλʔϯ͕ճ
 ·ͨ͸ճ࿈ଓ͢Δ΋ͷʹϚον ༻ྫ

    IUUQSVCVMBSDPNS %G"WW+[
  31. ? • 0ճ·ͨ͸1ճ • 2ճҎ্࿈ଓ͢Δ΋ͷʹ͸Ϛον͍ͯ͠ͳ͍

  32. + ߲໨ આ໌ ه߸ ಡΈํ ϓϥε ػೳ ௚લͷจࣈ΍ύλʔϯ͕ճҎ্ ࿈ଓ͢Δ΋ͷʹϚον ༻ྫ

    IUUQSVCVMBSDPNS S--(7L7
  33. + • 1ճҎ্ͳͷͰ௚લจࣈ͸লུͰ͖ͳ͍ • *ͱ?ͱͷҧ͍Λҙࣝ͢΂͠

  34. ؓ࿩ٳ୊ +ͱ͔*ͱ͔ʁͱ͔ɺ৭ʑ͋Δͷ͸Θ͔ͬͨΜͰ͚͢Ͳ ػೳ͕ࣅ͍֮ͯͯ͑ʹ͍͘ΜͰ͕͢ʝ

  35. ؓ࿩ٳ୊ ( ʈωʈ) ͜͜ʹJIS഑ྻͷ ɹɹɹΩʔϘʔυ͕ ɹɹɹ͋Δ͡ΌΖʁ

  36. ؓ࿩ٳ୊ ( ʈωʈ) ࣮͸͜͏͡Ό

  37. None
  38. {n} ߲໨ આ໌ ه߸ \O^ ಡΈํ ϒϨʔε
 ೾ׅހɺதׅހ ػೳ ௚લͷจࣈ΍άϧʔϓͷ


    OճҎ্ͷ܁Γฦ͠ʹϚον ༻ྫ IUUQSVCVMBSDPNS U[U,L+C
  39. {n} • {}಺ʹࢦఆͨ͠਺ͷจࣈʹϚον͢Δ • a{2}ͳΒa͕2ճ࿈ଓ͍ͯ͠Δ΋ͷʹϚον

  40. {n}ͷछྨ ճ਺ͷߜΓࠐΈ͕Ͱ͖ΔͷͰɺޮ཰త͔ͭ҆৺ײ͕͋Δ ߲໨ \O^ \O ^ \ O^ \O N^

    ػೳ
 ௚લͷจࣈΛ Oճ OճҎ্ OճҎԼ OճҎ্NճҎ Լ
  41. ·ͱΊ ճ਺ ه߸ ҙຯ  ճҎ্ͷ܁Γฦ͠ʹ Ϛον d ճҎ্ͷ܁Γฦ͠ʹ Ϛον

    PS ճ·ͨ͸ճͷ܁Γ ฦ͠ʹϚον ࢦఆճ਺ \O^ Oճͷ܁Γฦ͠ʹ Ϛον ࢦఆճ਺Ҏ্ \O ^ OճҎ্ͷ܁Γฦ͠ʹ Ϛον ࢦఆճ਺Ҏ্ɺࢦఆճ ਺ҎԼ \O N^ OճҎ্NճҎԼͷ ܁Γฦ͠ʹϚον
  42. (3)ෳ਺จࣈΛѻ͏΋ͷ

  43. ෳ਺ͷจࣈྻΛѻ͏ ϝλจࣈ ػೳ c cͰ۠੾ΒΕͨ
 ͍ͣΕ͔ͷจࣈྻʹϚον  ਖ਼نදݱͷάϧʔϓԽ <> จࣈΫϥεͷ࡞੒

    a  จࣈΫϥεͷলུه๏
  όοΫεϥογϡه๏
  44. | ߲໨ આ໌ ه߸ c ಡΈํ όʔςΟΧϧόʔ ػೳ cͰ۠੾ΒΕͨจࣈྻͷ͍ͣΕ͔ͷ จࣈྻʹϚον

    ༻ྫ IUUQSVCVMBSDPNS WB/F'NH
  45. | • |Ͱ۠੾ΒΕͨจࣈΛϚονͰ͖Δ • orͱಡΈସ͑ͯ΋͍͍ • A | B ͳΒAͱBʹϚον

  46. [ ] ߲໨ આ໌ ه߸ <> ಡΈํ େ͔ͬ͜ɺϒϥέοτ ػೳ Χοί಺ͷ͍ͣΕ͔ͷจࣈ

    ༻ྫ IUUQSVCVMBSDPNSJDR2[UQJ"
  47. [ ] • []಺ͷจࣈ͸ɺҰͭͷ·ͱ·ͬͨจࣈྻͰ͸ͳ͘ݸʑͷจࣈͱͯ͠ѻΘΕΔ • ͳͷͰԼهͷྫͩͱʮஉʯʮੑʯʮ͞ʯʮΜʯ͕ݸผʹϚον͢Δ

  48. [ ]೿ੜɿ^Λ͚ͭΔͱ൱ఆͷҙຯ ߲໨ આ໌ ه߸ <?> ಡΈํ ൱ఆ෇͖େ͔ͬ͜ɺϒϥέοτ ػೳ Χοί಺ͷ͍ͣΕ͔ͷ


    จࣈҎ֎ͷ΋ͷ ༻ྫ IUUQSVCVMBSDPNS J',4(3QBJO
  49. []ͷলུه๏ • []಺ͷจࣈҎ֎ͷ΋ͷ͕Ϛον͢Δ • ͳͷͰԼهͷྫͩͱʮஉʯʮੑʯʮ͞ʯʮΜʯҎ֎ͷ΋ͷ͕Ϛον͢Δ

  50. ( ) ߲໨ આ໌ ه߸  ಡΈํ ؙ͔ͬ͜ɺύʔϨϯ ػೳ άϧʔϓΛ࡞Δ

    ༻ྫ ߲࣍
  51. ( ) • []ͱҧ͍άϧʔϓͱͯ͠ѻ͏ͷͰॱং͕อͨΕΔ • ্هͷಛੑ͔Β | ͱ૬ੑ͕ྑ͍

  52. \ ߲໨ આ໌ ه߸ a ಡΈํ όοΫεϥογϡ ػೳ ɾϝλจࣈΛΤεέʔϓ͢Δ ɾa

    ಛఆͷӳࣈͰจࣈͷলུه๏Λ࡞Δ ༻ྫ IUUQSVCVMBSDPNSJDR2[UQJ"
  53. Τεέʔϓͬͯʁ • ਖ਼نදݱதͩͱɺ͋Δछͷه߸͸ϝλจࣈͱͯ͠ೝࣝ͞Εͯ͠·͏ • ϝλจࣈͱͯ͠ͷҙຯΛଧͪফ͢͜ͱΛΤεέʔϓͱ͍͏

  54. จࣈͷলུه๏ লུه๏ આ໌ লུݩ උߟ aX ӳ਺ࣈ <B[";@> XPSE a8

    ඇӳ਺ࣈ <?B[";@> 803% aT ۭനจࣈ <aUaSaOaGaW> TQBDF a4 ඇۭനจࣈ <?aUaSaOaGaW> 41"$& aE ਐ਺ࣈ <> EJHJU a% ඇਐ਺ࣈ <?> %*(*5 aI ਐ਺ࣈ <BG"'> IFYBEFDJNBM OVNCFS a) ඇਐ਺ࣈ <?BG"'> )&9"%&$*."- /6.#&3 Մಡੑ͕͕͋ΔͷͰͥͻ࢖͍͖ͬͯ·͠ΐ͏
  55. লུه๏ɺ৭ʑ͋Δ͚Ͳ4छྨ • େจࣈͩͱʮҎ֎ʯʹͳΔΑ • ӳ਺ࣈ͔ ۭനจࣈ͔(w ͱ s) • 10ਐ਺͔

    16ਐ਺͔(d ͱ h )
  56. ࠶ܝ ࠷ॳʹோΊͨ࣌ͱൺ΂ΔͱɺͪΐͬͱΘ͔ΔΑ͏ʹͳͬͯ·ͤΜ͔ʁ /P ϝλจࣈ  ?
    

        \^  c    <>  a
  57. ͔֮ͤͬ͑ͨ͘Μ͔ͩΒ
 ͳΜ͔Ϳͬ͜͵͍ͯΈ͍ͨ

  58. grep • grepͯ͠ΈΔ • StringΫϥε͕࣋ͬͯΔΠϯελϯεϝιουͱ͔

  59. match • ࠓ࡞ۀ͠ͳ͕Βௌ͍ͯΔYoutubeͷύϥϝʔλͱ͔

  60. gsub • ൵͍͠աڈΛٹࡁͯ͋͛͠Δͱ͔ʝ

  61. ਖ਼نදݱɺศརͰ͢Ͷʂ ৭ʑ࢖͍͖ͬͯ·͠ΐ͏ʂʂ