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
730
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
180
Enhancing Applications with Accessibility API
kishikawakatsumi
3
3.4k
Mastering SwiftSyntax
kishikawakatsumi
4
5.6k
My SwiftData Review
kishikawakatsumi
7
1.4k
Swift Expression Macros: a practical introduction
kishikawakatsumi
3
1.9k
Xcode Cloudの評価
kishikawakatsumi
2
1.3k
家のいろいろな数値を計測する
kishikawakatsumi
4
2.2k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1.1k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1.9k
Other Decks in Programming
See All in Programming
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
2
490
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
800
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
780
為你自己學 Python
eddie
0
460
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
330
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
310
Spatial Rendering for Apple Vision Pro
warrenm
0
270
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
990
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
160
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
210
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
430
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
How GitHub (no longer) Works
holman
311
140k
Building Applications with DynamoDB
mza
92
6.1k
Unsuck your backbone
ammeep
669
57k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
A Philosophy of Restraint
colly
203
16k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
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/