Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

似たもの同士のPerlとPHP

uzulla
November 29, 2024

 似たもの同士のPerlとPHP

2024-11-30
紅白ぺぱ合戦 : ぺぱBaanGaai LT
https://connpass.com/event/329428/

uzulla

November 29, 2024
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. Τϯδϯ͸ࣅͯΔ͕ɺΠϯλʔϑΣʔε͕ҧ͏ w 1FSM͸͜ΜͳϦςϥϧ ͕͋Δ $txt = "ABC"; $txt =~ s/A/a/;

    print $txt; w 1)1͸͜Μͳؔ਺Λ࢖͏ $txt = preg_replace("/A/u", "a", "ABC"); echo $txt; w *'Ҏ֎͸ͦͬ͘Γʂʂʂ
  2. Ωϟϓνϟͷྫ1)1 preg_match( '| (?P<scheme> https?) # SchemeΛΩϟϓνϟ :// # "://"

    (?P<host> [^/\s]+) # ϗετ໊ΛΩϟϓνϟ |x', "https://test.com/", $matches ); $scheme = $matches['scheme']; $host = $matches['host'];
  3. Ωϟϓνϟͷྫ1FSM $input = "https://test.com"; $input =~ m{ (?<scheme> https?) #

    SchemeΛΩϟϓνϟ :// # "://" (?<host> [^/\s]+) # ϗετ໊ΛΩϟϓνϟ }x; my $scheme = $+{scheme}; # ͕݁͜͜ߏΫη͔ͳʁ my $host = $+{host};
  4. ஔ׵ͷྫ1FSM $input =~ s/abc/ do { # ͜ͷதʹίʔυ͕ॻ͚Δ my $matched

    = $&; # Ωϟϓνϟͨ͠΋ͷΛͱΓͩ͠ uc($matched); # ॲཧΛॻ͖ɺ࠷ޙͷධՁ͕ஔ׵ޙʹ } /ge; # ʮҎԼͰ͑͑΍Ζʯʮ͔ͨ͠ʹʯ(ucͱ͍͏func͕͋Γ·͢) $input =~ s/(abc)/uc($1)/ge;
  5. දʹͯ͠ΈΔ ಛ௃ 1FSM 1)1 جຊࢀর a a a aʢಉ͡ʣ ໊લ෇͖ఆٛ

    (?<name>pattern) (?P<name>pattern) ໊લ෇͖ࢀর \k<name> \k'name' (?P=name) Ωϟϓνϟ݁Ռ $1 $2͕ࣗಈͰએݴɻ ໊લ෇͖ͩͱ$+{name} Ҿ਺ʹ$matchesͳͲΛࢦఆ ͢Δͱ࿈૝഑ྻ͕એݴ͞ΕΔ ࣗಈม਺ $_ ػೳͱͯ͠ͳ͍
  6. தؒ·ͱΊ w ૬ޓͰ௕ॴ୹ॴ͸͋Δ ʁʁʁ w ͪΐͬͱؾΛ෇͚Ε͹૬ޓͰΘ͔Γ͋͑ͦ͏ w ͔ͩΒ1FSMͱ1)1͸૬ੑ͕ʜྑ͍ʂʂʂ ॾઆ͋Δ w

    ͔͠͠ͳ͕Β΍͸Γݴޠ͕ҧ͑͹ࡉ͔͍ࠩ͸͋Δ w 1)1FSͱ1FSM.POHFS͕͓ޓ͍ʹ͢ΕҧΘͳ͍ͨΊʹ΋ʜ
  7. ౴͑" 1FSM ղઆ1FSM͸σϑΥϧτͰυοτ ͕վߦʹϚον͠·͢ɻͭ·Γɺෳ਺ߦʹ·ͨ ͕ΔจࣈྻશମʹϚον͠·͢ɻ 1)1Ͱ͸σϑΥϧτͰυοτ ͸վߦʹϚον͠ͳ͍ͨΊɺෳ਺ߦʹ·͕ͨΔϚ ονϯάΛߦ͏ʹ͸sϑϥάʢ1$3&@%05"--

    υοτΦʔϧϞʔυʣ͕ඞཁͰɺ ςετ͕ශऑͳίʔυͰ͠͹͠͹͜ͷόάΛݟ͔͚·͢ɻ php > preg_match_all("/.+/", "hello\nworld", $m); var_dump($m); array(1) { [0]=> array(2) { [0]=> string(5) "hello" [1]=> string(5) "world" } }
  8. 2ϩάղੳύλʔϯ Regex: m/^(\d{4}-\d{2}-\d{2}).*/ Input: 2024-01-01 Error 2024-01-02 Warning Expected: 2ߦϚον

    Actual: 1ߦϚον όάͷݪҼ͸ʁ A) PerlͰ/mϑϥά͕ඞཁ B) PHPͰmϑϥάΛ࡟আ͢Δඞཁ͕͋Δ C) ೖྗͷվߦίʔυ͕ҧ͏
  9. 2ᩦཉϚονϯά Regex: m/a.*b/ Input: "a" + "x" * 1000000 +

    "b" Result: ελοΫෆ଍΍ϝϞϦෆ଍ ૣ͘Τϥʔʹͳͬͯ͠·͏ͷ͸ʁ A) Perl B) PHP C) ࠩ͸ͳ͍