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
720
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
160
Enhancing Applications with Accessibility API
kishikawakatsumi
3
3.2k
Mastering SwiftSyntax
kishikawakatsumi
4
5.3k
My SwiftData Review
kishikawakatsumi
7
1.4k
Swift Expression Macros: a practical introduction
kishikawakatsumi
3
1.8k
Xcode Cloudの評価
kishikawakatsumi
2
1.2k
家のいろいろな数値を計測する
kishikawakatsumi
4
2.2k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1.9k
Other Decks in Programming
See All in Programming
最新TCAキャッチアップ
0si43
0
190
Click-free releases & the making of a CLI app
oheyadam
2
120
距離関数を極める! / SESSIONS 2024
gam0022
0
280
cmp.Or に感動した
otakakot
3
200
CSC509 Lecture 11
javiergs
PRO
0
180
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
初めてDefinitelyTypedにPRを出した話
syumai
0
420
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
620
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
43
13k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
What's new in Ruby 2.0
geeforr
343
31k
Building an army of robots
kneath
302
43k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Producing Creativity
orderedlist
PRO
341
39k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Become a Pro
speakerdeck
PRO
25
5k
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/