Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Regular expressions basics/正規表現の基本
Search
Kishikawa Katsumi
July 26, 2022
Programming
7
710
Regular expressions basics/正規表現の基本
Regular expressions basics/正規表現の基本
Kishikawa Katsumi
July 26, 2022
Tweet
Share
More Decks by Kishikawa Katsumi
See All by Kishikawa Katsumi
iOSDC 2024 SMBファイル共有をSwiftで実装する
kishikawakatsumi
1
150
Enhancing Applications with Accessibility API
kishikawakatsumi
3
3.1k
Mastering SwiftSyntax
kishikawakatsumi
4
5.2k
My SwiftData Review
kishikawakatsumi
7
1.3k
Swift Expression Macros: a practical introduction
kishikawakatsumi
3
1.8k
Xcode Cloudの評価
kishikawakatsumi
2
1.2k
家のいろいろな数値を計測する
kishikawakatsumi
4
2.1k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
990
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1.8k
Other Decks in Programming
See All in Programming
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.5k
僕がつくった48個のWebサービス達
yusukebe
18
17k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
420
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
200
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
7
2.7k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
offers_20241022_imakiire.pdf
imakurusu
2
360
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.6k
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.3k
Piniaの現状と今後
waka292
5
1.4k
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.1k
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
GitHub's CSS Performance
jonrohan
1030
460k
Into the Great Unknown - MozCon
thekraken
31
1.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Fireside Chat
paigeccino
32
3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
How to Ace a Technical Interview
jacobian
275
23k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Why Our Code Smells
bkeepers
PRO
334
57k
Transcript
ਖ਼نදݱͷجຊ Regular expressions basics
Swift Regex
https://swiftregex.com/
ਖ਼نදݱͱ • จࣈྻͷू߹ʢύλʔϯʣΛද͢൚༻తͳه๏ • [bc]ookbook·ͨcookʹϚον͢Δ
Ϧςϥϧ Literal Characters • a • Jack is a boy,
• cat • About cats and dogs
ϝλจࣈ Special Characters (Meta Characters) • ಛผͳҙຯΛ࣋ͭ12ͷจࣈʢϦςϥϧͱͯ͠ѻ͏ʹΤεέʔϓ͕ඞཁɻʢྫʣ1\+1=2ʣ • όοΫεϥογϡ \
• ΩϟϨοτ ^ • υϧϚʔΫ $ • υοτʢϐϦΦυʣ . • ύΠϓ | • ΫΤενϣϯϚʔΫ ? • ΞελϦεΫ * • ϓϥε + • ։ؙ͖Χοί ( • ดؙ͡Χοί ) • ։͖֯Χοί [ • ։͖Χοί {
੍ޚจࣈ Non-Printable Characters (Control Characters, Escape sequence) • \t •
λϒʹϚον͢Δ • \n • վߦʹϚον͢Δ
จࣈΫϥε Character Classes (Character Sets) • ෳͷจࣈͷத͔Β̍ͭͷจࣈʹϚον͢Δ • a·ͨeʹϚονͤ͞Δʹ[ae]ͱॻ͘ •
ʢྫʣgr[ae]y • gray·ͨgreyʹϚον • จࣈΫϥε1ͭͷจࣈʹϚον͢Δ • Χοίͷதͷจࣈͷॱংؔͳ͍
จࣈΫϥε Character Classes (Character Sets) • จࣈΫϥεͷதͰϋΠϑϯΛ༻ͯ͠ൣғΛࢦఆͰ͖Δ • [0-9] •
͔̌Β̕ͷؒͷܻ̍ͷࣈʹϚον • [0-9a-fA-F] • େจࣈͱখจࣈΛ۠ผͤͣʹɺ16ਐͷࣈܻ̍ʹϚον • ൱ఆจࣈΫϥεʢNegated Character Classesʣ • [^0-9\r\n] • ࣈ·ͨվߦͰͳ͍ҙͷจࣈʹϚον
จࣈΫϥεʢͷུه๏ʣ Shorthand Character Classes • จࣈΫϥεͷதͰΑ͘ΘΕΔͷΛ؆୯ʹॻ͚ΔΑ͏ʹ͋Β͔͡Ί༻ҙ͞Εͨه๏ • \d[0-9]ͷུه๏ • UnicodeΛαϙʔτ͢ΔڥͰࣈؙࣈͳͲͯ͢ͷࣈʹϚον
• \w “word character” [A-Za-z0-9_]ͱಉ͡ʢΞϯμʔείΞؚ͕·ΕΔ͜ͱʹҙʣ • UnicodeΛαϙʔτ͢ΔڥͰ͍Ζ͍ΖͳจࣈʹϚον • \s “whitespace character” ۭനจࣈʹϚον [ \t\r\n\f] • UnicodeΛαϙʔτ͢ΔڥͰUnicodeͷʮseparatorʯΧςΰϦͷͯ͢ͷจࣈʹϚον
υοτʢϐϦΦυʣ The Dot Matches (Almost) Any Character • վߦจࣈΛআ̍͘จࣈʹϚον •
“dot matches all”·ͨ“single line”Ϟʔυʢϓϩάϥϛϯάݴޠਖ਼نදݱΤ ϯδϯʹΑͬͯݺͼํҟͳΔʣΛࢦఆ͢ΔͱվߦจࣈΛؚΉҙͷ1จࣈʹ Ϛον • gr.yɺgrayɺgrayɺgr%yͳͲʹϚον • υοτڧྗʹͳΜͰϚον͢ΔͷͰ͍͗͢ͳ͍ • จࣈΫϥε൱ఆจࣈΫϥεΛΘΓʹ͏
ΞϯΧʔ Anchors • จࣈͰͳ͘ҐஔʹϚον • ^ • จࣈྻͷઌ಄ʹϚον • $
• จࣈྻͷඌʹϚον • ΄ͱΜͲͷਖ਼نදݱ“multi-line”Ϟʔυ͕͋Γɺ ^վߦͷޙΖɺ $վߦͷલʹϚον͢Δ • \b • ୯ޠڥքʹϚον • ୯ޠڥքͱ\wͰϚονͰ͖ΔจࣈͱɺͰ͖ͳ͍จࣈͷؒͷҐஔ
બ Alternation • ཧʢORʣ • cat|dog • About cats and
dogs • cat|dog|mouse| fi sh • ͖ͳ͚ͩͭͳ͛Δ͜ͱ͕Ͱ͖Δ • cat|dog food • cat·ͨdog foodʹϚον • cat food͔dog foodʹϚονͤ͞Δʹɺ(cat|dog) foodͷΑ͏ʹબΛάϧʔϓԽ͢Δ
܁Γฦ͠ Repetition • ΫΤενϣϯϚʔΫʮ?ʯ • Optional • colou?rcolor·ͨcolourʹϚον • ΞελϦεΫʮ*ʯ
• ̌ճҎ্ͷ܁Γฦ͠ • <[A-Za-z][A-Za-z0-9]*> • ଐੑ͕ͳ͍HTMLλάʹϚον • ϓϥεʮ+ʯ • ̍ճҎ্ͷ܁Γฦ͠ • Χοίʮ{n,m}ʯ • ࢦఆճͷ܁Γฦ͠ • \b[1-9][0-9]{3}b • 1000͔Β9999ͷࣈʹϚον • \b[1-9][0-9]{2,4}\b • 100͔Β99999ͷࣈʹϚον
άϧʔϓͱΩϟϓνϟ Grouping and Capturing • ΧοίͰғΉͱάϧʔϓԽ͞ΕΔ • άϧʔϓʹରͯ͠܁Γฦ͠ΛࢦఆͰ͖Δ • Set(Value)?
• Set·ͨSetValueʹϚον • ௨ৗͷؙΧοίΩϟϓνϟάϧʔϓΛ࡞͢Δ • Set(Value)?ͷਖ਼نදݱͰSetValue͕Ϛονͨ͠߹ɺάϧʔϓ̍ʹΞΫηε͢ΔͱValue͕औΓग़ͤΔ • Ωϟϓνϟ͕ඞཁͳ͍߹Set(?:Value)?ͱ͢ΔͱΩϟϓνϟ͠ͳ͍άϧʔϓ͕࡞Ͱ͖Δ • ؙΧοίͷޙͷΫΤενϣϯϚʔΫͱɺ̌ճҎ্ͷ܁Γฦ͠ͷࢦఆͷΫΤενϣϯϚʔΫΛࠞಉ͠ͳ͍Α͏ʹ ҙ
ޙํࢀর Backreferences • ΩϟϓνϟάϧʔϓͰΩϟϓνϟʢϚονʣͨ͠༰ʹϚον • ΩϟϓνϟάϧʔϓʹϚονͨ݁͠ՌΛ࠶ར༻Ͱ͖Δ • <([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1> • HTMLλάʹϚονʢΩϟϓνϟάϧʔϓʹϚονͨ͠։࢝λάΛऴྃλάͰ
࠶ར༻͍ͯ͠Δʣ
໊લ͖άϧʔϓʢΩϟϓνϟʣͱޙํࢀর Named Groups and Backreferences • ΩϟϓνϟͷࢀরΛ൪߸Ͱཧ͢Δͷେมͩ͠ɺՃআͰͣΕΔͷͰ໊લΛ͚ΒΕΔ • ߏจʢ໊લ͖άϧʔϓʣ •
(?P<name>group) • ߏจʢޙํࢀরʣ • (?P=name) • <(?P<tag>[A-Z][A-Z0-9]*)\b[^>]*>.*?</(?P=tag)> • HTMLλάʹϚονʢ <([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>ͱಉ͡ʣ • ߏจʢ໊લ͖Ωϟϓνϟʢ.NETʣʣ • (?<name>group)·ͨ(?’name'group) • ߏจʢ໊લʹΑΔࢀরʢ.NETʣʣ • \k<name>·ͨ\k'name'
ઌಡΈͱޙಡΈ Lookaround (Lookahead/Lookback(Lookbehind)) • ಛघͳάϧʔϓͰɺΞϯΧʔͷΑ͏ʹϚονͨ݁͠ՌͷҐஔΛࢦఆ͢Δ • ʢྫʣ\d+(?=€) • ͷޙʹʮ€ʯ͕ଓ͘จࣈྻʹϚον •
1 turkey costs 30€ͷ30ʹϚον • ߏจʢߠఆઌಡΈʢPositive lookaheadʣʣ • X(?=Y) • ߏจʢ൱ఆઌಡΈʢNegative lookaheadʣʣ • X(?!Y) • ߏจʢߠఆޙಡΈʢPositive lookbehindʣʣ • (?<=Y)X • ߏจʢ൱ఆޙಡΈʢNegative lookbehindʣʣ • (?<!Y)X
References • Regular-Expressions.info https://www.regular-expressions.info/ • Swift Regex https://swiftregex.com/