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
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
初めてDefinitelyTypedにPRを出した話
syumai
0
400
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Featured
See All Featured
Done Done
chrislema
181
16k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Optimising Largest Contentful Paint
csswizardry
33
2.9k
It's Worth the Effort
3n
183
27k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
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/