$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
似たもの同士のPerlとPHP
Search
uzulla
November 29, 2024
Programming
1
100
似たもの同士のPerlとPHP
2024-11-30
紅白ぺぱ合戦 : ぺぱBaanGaai LT
https://connpass.com/event/329428/
uzulla
November 29, 2024
Tweet
Share
More Decks by uzulla
See All by uzulla
More Context, Better Code. 既存コードやOAS等をコンテキストとしてLLMに与える事で、よりよいコード生成を行う話
uzulla
1
110
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
3
170
セッションのトークセッション / Traps for PHP session features in growing web apps
uzulla
2
110
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
5
2k
例外を投げるのをやめてみないか? あるいは受け入れてみないか? - How to use exceptions other than throwing
uzulla
4
910
PHPerが ISUCONでやるべき事
uzulla
1
910
開発生産性は上がらない - N Ways to Reduce Development Productivity
uzulla
1
230
test is not a job
uzulla
1
490
あなたのPHPアプリ、ログはでてますか?あるいはログをだしてますか? / Are you writing a log? Or just out a log?
uzulla
17
7.5k
Other Decks in Programming
See All in Programming
macOS なしで iOS アプリを開発する(※ただし xxx に限る)
mitsuharu
1
150
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
3.7k
N.E.X.T LEVEL
pluu
2
220
layerx_20241129.pdf
kyoheig3
1
180
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
120
PipeCDの歩き方
kuro_kurorrr
3
140
初めてDefinitelyTypedにPRを出した話
syumai
0
480
WebAssembly Unleashed: Powering Server-Side Applications
chrisft25
0
2k
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
240
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
830
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
410
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
150
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
1
220
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.2k
Docker and Python
trallard
40
3.1k
The Cult of Friendly URLs
andyhume
78
6.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Embracing the Ebb and Flow
colly
84
4.5k
Into the Great Unknown - MozCon
thekraken
33
1.5k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Transcript
ࣅͨͷಉ࢜ͷ 1FSMͱ1)1 w ߚനͺ߹ઓͺ#BBO(BBJ-5 w IUUQTDPOOQBTTDPNFWFOU
ࣗݾհ w 1FSM.POHFSʹ1)1ͷਓɺ1)1ͷਓʹ1FSMͷਓͱ ͍ΘΕΔίϞϦͷV[VMMBͰ͢ w )BDIJPKJQNͱ͍͏QN 1FSM.POHFSͷ.FFUVQ Λओ࠵͍ͯ͠·͢ w ͱ͍͑ɺ࠷ۙΊ͖ͬΓ1FSMͷਓͱࢥΘΕͳ
͘ͳΓ·ͨ͠Ͷʜ w ͜ͷͰإ͕͍ํ Β͍͠ ͳͷͰɺനࠇ͔ͭ ͳ͍άϨʔκʔϯύʔιϯͱͯ͜͜͠ʹ͍ΔͬΆ ͍Ͱ͢
ͱ͍͏͜ͱͰʜ w 1FSMͱ1)1ͰΑ͘ࣅͯΔͷͱ͍͑ʜ
None
w ʮΈΜͳ͖ͩΖʯʮͦ͏͔ʁʁʯ w ʮ৭͕פ৭ܥʯʮʁʁʁʯ
w ਖ਼نදݱͰ͢Ͷʂʂʂ w 1)1ͷਖ਼نදݱ1$3& 1FSM$PNQBUJCMF3FHVMBS&YQSFTTJPOT w ʮޓੑ͕͋Γͦ͏ʂʂʂʯ w ࣮ࡍͦͬ͘Γʂʂ ʁ
ʮ͑ͬʯ
ΤϯδϯࣅͯΔ͕ɺΠϯλʔϑΣʔε͕ҧ͏ w 1FSM͜ΜͳϦςϥϧ ͕͋Δ $txt = "ABC"; $txt =~ s/A/a/;
print $txt; w 1)1͜ΜͳؔΛ͏ $txt = preg_replace("/A/u", "a", "ABC"); echo $txt; w *'Ҏ֎ͦͬ͘Γʂʂʂ
Ωϟϓνϟͷྫ1)1 preg_match( '| (?P<scheme> https?) # SchemeΛΩϟϓνϟ :// # "://"
(?P<host> [^/\s]+) # ϗετ໊ΛΩϟϓνϟ |x', "https://test.com/", $matches ); $scheme = $matches['scheme']; $host = $matches['host'];
Ωϟϓνϟͷྫ1FSM $input = "https://test.com"; $input =~ m{ (?<scheme> https?) #
SchemeΛΩϟϓνϟ :// # "://" (?<host> [^/\s]+) # ϗετ໊ΛΩϟϓνϟ }x; my $scheme = $+{scheme}; # ͕݁͜͜ߏΫη͔ͳʁ my $host = $+{host};
ஔͷྫ1FSM $input =~ s/abc/ do { # ͜ͷதʹίʔυ͕ॻ͚Δ my $matched
= $&; # Ωϟϓνϟͨ͠ͷΛͱΓͩ͠ uc($matched); # ॲཧΛॻ͖ɺ࠷ޙͷධՁ͕ஔޙʹ } /ge; # ʮҎԼͰ͑͑Ζʯʮ͔ͨ͠ʹʯ(ucͱ͍͏func͕͋Γ·͢) $input =~ s/(abc)/uc($1)/ge;
ஔͷྫ1)1 preg_replace_callback("/abc/", function($matches) { return strtoupper($matches[0]); }, $input); w ศརػೳɺΠϯϥΠϯจࣈྻ࿈݁Մೳͳ&WBMةݥͳͷͰফ͑·ͨ͠
# php<=5.4 ϑϥάʹeΛ͚ͭΔͱevalͰ͖ͨ $func = "strtoupper"; preg_replace("/abc/e", $func."('\\0')", $input);
දʹͯ͠ΈΔ ಛ 1FSM 1)1 جຊࢀর a a a aʢಉ͡ʣ ໊લ͖ఆٛ
(?<name>pattern) (?P<name>pattern) ໊લ͖ࢀর \k<name> \k'name' (?P=name) Ωϟϓνϟ݁Ռ $1 $2͕ࣗಈͰએݴɻ ໊લ͖ͩͱ$+{name} Ҿʹ$matchesͳͲΛࢦఆ ͢Δͱ࿈ྻ͕એݴ͞ΕΔ ࣗಈม $_ ػೳͱͯ͠ͳ͍
ͦͬ͘ΓͰ͢Ͷʂ w ਖ਼نදݱࣗମͷه๏΄΅ҰॹͳΜΑͶɺߏจɺͷग़͠ೖΕͷํ๏͕ͪΐͬ ͱҧ͏͘Β͍ɻ `preg_replace` <=> `s///` `preg_match` <=> `m///`
w Ͳ͕͖ͬͪͰ͔͢ʁ
w 1)1FS ʮͳΜ͔ʜ1FSMͷํ͕͓͠ΌΕͩͳΝʂʯ w Ұൠ௨ա1)1FS ʮ͓͠ΌΕʜʁͨͱ͑͜͏͔ʁʯ $_ = "https://example.jp"; if
(m<https?://(?<host>[^/]+)>) { print "Host: $+{host}\n"; } w 1)1FS ʮ͓͠ΌΕʂʯ
தؒ·ͱΊ w ૬ޓͰॴॴ͋Δ ʁʁʁ w ͪΐͬͱؾΛ͚Ε૬ޓͰΘ͔Γ͋͑ͦ͏ w ͔ͩΒ1FSMͱ1)1૬ੑ͕ʜྑ͍ʂʂʂ ॾઆ͋Δ w
͔͠͠ͳ͕ΒΓݴޠ͕ҧ͑ࡉ͔͍ࠩ͋Δ w 1)1FSͱ1FSM.POHFS͕͓ޓ͍ʹ͢ΕҧΘͳ͍ͨΊʹʜ
ΫΠζ͠·͠ΐ͏ʂʂ Կ൪Ḧͩ͜͡Εʁʁʁ
2ߦͷઌ಄Ϛον Regex: m/^Second/ Input: "First line\nSecond line" Result: Ϛον͋Γ A)
Perl B) PHP C) ྆ํ
͑# 1)1 ղઆ1)1σϑΥϧτͰ֤ߦͷઌ಄ʹϚον͠·͢ɻ1FSMͰσϑΥϧτͰจࣈ ྻશମͷઌ಄ʹ͔͠Ϛον͠·ͤΜɻ1FSMͰ֤ߦͷઌ಄ʹϚονͤ͞Δʹɺmϑ ϥάʢϚϧνϥΠϯϞʔυʣ͕ඞཁͰ͢ɻ
2ෳߦϚονϯά Regex: m/.+/ Input: "hello\nworld" Result: Ϛον༗Γɺൣғ "hello\nworld" A) Perl
B) PHP C) ྆ํ
͑" 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" } }
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) ೖྗͷվߦίʔυ͕ҧ͏
͑" 1FSMͰNϑϥά͕ඞཁ ղઆ1FSMͰϩάϑΝΠϧղੳΛߦ͏ࡍɺ^͕֤ߦͷઌ಄ʹϚον͢ΔΑ͏ʹ͢Δʹ mϑϥάʢϚϧνϥΠϯϞʔυʣ͕ඞཁͰ͢ɺͦ͏͠ͳ͍ͱจࣈྻͷઌ಄ʹͷΈ Ϛονͯ͠͠·͍·͢ɻ 1)1ͰσϑΥϧτͰ֤ߦͷઌ಄ʹϚον͠·͢ɻ ͕ɺͦͷ͍ͤͰվߦΛߟྀ͠ͳ͍ ͰΑ͍ͱ৴͡ࠐΜͩ ࣌ʹ^$ͷ᠘ ͠͠ɺΫ
ϦςΟΧϧͳόϦσʔγϣϯϛεʹͳΔ ͕ଟ͘ɺେͷ߹\A\z จࣈྻͷઌ ಄ͱɺऴ Λ͏͖ͩͱࢲࢥ͍·͢ ॾઆ͋Δ
26OJDPEFจࣈΫϥε Regex: m/\w+/ Input: "͜Μʹͪ" Result: No Match A) Perl
B) PHP C) ྆ํ
͑$ ྆ํ ղઆ1FSMͰ\w͕σϑΥϧτͰ"4$**จࣈʹͷΈϚον͠·͢ɻ 6OJDPEFจࣈʹϚονͤ͞Δʹɺίʔυʹͯuse utf8;ϓϥάϚΛ༻͢Δ ͷඞཁ͕͋Γ·͢ɻ 1FSM.POHFSʮࢦఆ͠ͳ͍ͱ͔͋Γ͑ͳ͍ͩΖɺഅࣛʹͯ͠Δͷ͔ʯ ࢲʮŰŘŕʯ ·ͨɺ1)1ͰจࣈྻόΠφϦͱͯ͠ѻΘΕΔͷͰɺ"/\w+/u",ͱVUGϑϥ άΛ͚ͭ·͢ɻ
1)1FSʮ͜͜ʹຊޠ͜ͳ͍͠ʯ ീԦࢠࡏॅ1)1FSʮ͍͍͔ΒVΛ͚ͭͱ͚ʯ
2େจࣈখจࣈม Regex: s/(\w+)/\U$1/g Input: "hello world" Result: "HELLO WORLD" A)
Perl B) PHP C) ྆ํ
͑" 1FSM ղઆ1FSMΈࠐΈͷ\UมΛαϙʔτ͓ͯ͠Γɺਖ਼نදݱͷஔͰେจࣈม ͕ՄೳͰ͢ɻ ਖ਼نදݱͱ͍͍ͭͭɺػೳతʹਖ਼نදݱͰͳΜͰͳ͘ɺจࣈྻॲཧͰ͢ ͕ʜ \uɺจࣈ͚ͩେจࣈʹ͢Δͱ͔Ͱ͖ͯศར IFMMP)FMMP ʜͦ͏ʜ͚ͩͲͭ ͔ͬͨ͜ͱͳ͍ͳʁ
1)1Ͱಉ༷ͷมΛߦ͏ʹpreg_replace_callbackؔΛ༻ͯ͠ίʔϧ όοΫͰstrtoupperؔΛݺͼग़͢ඞཁ͕͋Γ·͢ɻ
2ޙํࢀর Regex: m/(<([a-z]+)>.*?</\2>)/ Input: "<div>content</div>" Result: Match! A) Perl B)
PHP C) ྆ํ
͑$ ྆ํ ղઆʹΑΔޙํࢀর1FSMͱ1)1ͷ྆ํͰαϙʔτ͞Ε͍ͯ·͢ɻ ͲͪΒͷݴޠͰɺΩϟϓνϟάϧʔϓͷ൪߸Λ༻ͯ͠ޙํࢀরΛߦ͏͜ͱ͕Ͱ ͖·͢ɻ ͕͢͞ʹ͜Ε͘Β͍Ͱ͖ͳ͍ͱ͓͔͍͠Ͱ͢ΑͶɻ
2ίϝϯτߏจ Regex: /(?#͜ͷ෦ίϝϯτ)abc/x Input: "abc" Result: Match! ͜Ε ͲͪΒʁ A)
Perl B) PHP C) ྆ํ
͑$ ྆ํ ղઆ༧֎͔͠Ε·ͤΜ͕ɺ1FSMͱ1)1ͷ྆ํͰαϙʔτ͞Ε͍ͯ·͢ɻ 1FSMͷํ͕ΑΓॊೈͰ͕͢ɺ·͋ͦΜͳෳࡶͳίϝϯτͤΜΖɻͦΜͳෳࡶͳ ΒɺYϑϥά͔ͭͬͯͰίϝϯτͨ͠΄͏͕͍ٓ͠ɻ
2݅ذ Regex: m/(a)?b(?(1)c|d)/ Input: "abc" Result: Match! ͜Ε ͲͪΒʁ A)
Perl B) PHP
͑$ ྆ํ ղઆ͖݅Ϛονϯά1FSMͱ1)1ͷ྆ํͰαϙʔτ͞Ε͍ͯ·͢ɻ m/(a)?b(?(1)c|d)/ ͜ͷέʔεͩͱɺB ʹΩϟϓνϟάϧʔϓ Λ͔ͭͬͯD EͷϚον݅Λ੍ޚͯ͠ ͓ΓɺBCDɺ͋Δ͍CEʹϚον͠·͢ɻ ͕ɺ1)1FSଟͬͨ͜ͱ͕ແ͍ͱࢥ͍·͢ɻ
2ᩦཉϚονϯά Regex: m/a.*b/ Input: "a" + "x" * 1000000 +
"b" Result: ελοΫෆϝϞϦෆ ૣ͘Τϥʔʹͳͬͯ͠·͏ͷʁ A) Perl B) PHP C) ࠩͳ͍
͑# 1)1 ղઆجຊతʹ1FSMͱ1)1ͷ྆ํͰɺᩦཉϚονϯάେ͖ͳೖྗͰελοΫΦ ʔόʔϑϩʔϝϞϦ੍ݶա͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ɻ ͨͩ͠ɺ1)1ͷํ͕ૣ͘ΤϥʔʹͳΔͣͰ͢ɻ ͳͥͳΒΦʔόʔϑϩʔ͢ΔΑ͏ͳίʔυେ1)1FSͷϛεͳͷͰɺͬ͞͞ͱऴ ྃͯ͠ඃΛ࠷খݶʹ͠·͢ɻ
2όοΫτϥοΩϯά Regex: m/(a|aa|aaa|aaaa|aaaaa|aaaaaa)*/ Input: "aaaaa" ... (a͕݁ߏࢁ Result: ࣦഊ(Ϛονͤͣ) ͜Ε
ͲͪΒʁ A) ͲͪΒ͔ͱ͍͑Perl B) ͲͪΒ͔ͱ͍͑PHP
͑# ͲͪΒ͔ͱ͍͑1)1 ղઆ1)11)1FSΛ৴͍ͯ͡ͳ͍ͷͰɺ΄͔ͷϓϩηεʹ໎Λ͔͚ͳ͍Α͏ ʹɺͬ͞͞ͱ༏͘͠ڧ੍ऴྃ͠·͢ɻ όοΫτϥοΩϯάͷ੍ݶQIQJOJͰௐ͢Δ͜ͱͰ͖·͢ɻ # Α͘ղΒͳ͍͚Ͳ͜ΕΛͬͨΒͳ͓ͬͨɺফ͞ͳ͍Ͱ͍ͩ͘͞ ini_set('pcre.backtrack_limit', 99999999999); (*SKIP)(*FAIL)ͷςΫ͋Γ·͕͢ɺ1)1FS͍·ͤΜ
ʁʁʁ 1FSMόοΫτϥοΩϯά੍ݶ͕ͳ͘ɺݶք·Ͱ͕ΜΓ·͢ɻ ͭ·Γ1FSM1FSM.POHFSΛա৴͍ͯ͠ΔͷͰɺ͠͠NPE@QFSM͕མͪ·͢ ݹ
͍͔͕Ͱ͔ͨ͠ʁ w ΫΠζʹͰ͖Δఔʹɺࠩେ͖͘ͳ͍Ͱ͢ΑͶʁ w ͔͠͠ɺվߦΈ͍ͨʹ୯७ͳͰڍಈ͕ࠩ͋ΔͷͰؾΛ͚ͭͳ͖Ό͍͚·ͤΜ w 1FSM؆ܿʹॻ͚ͯύϫϑϧͰ͕͢ɺ͍֮͑ͯͳ͍ͱॻ͖ʹ͍͘ॴ͕͋Γ·͢ ͠ɺϛεΔͱ00.͠·͢ɻ w 1)1γϯϓϧͰෛՙʹϑΣΠϧηʔϑ͕͋Γ·͕͢ɺίʔυखଓ͖తͰ͢
͠ɺݶք·Ͱ͍͍ͨ࣌QIQJOJͷௐ͕ඞཁͰ͢ɻ w ͱ͍͑ࠜͬ͜ࣅ͍ͯΔͷͰɺ૬ޓͰཧղ͢Δͷ͘͠ͳ͍Ͱ͠ΐ͏ʂ ɹ͓ޓ͍͔֬Ίͳ͕Β͍͖ͬͯ·͠ΐ͏ʂ w ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ