Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
正規表現エンジンを自作した話
Search
北村
August 23, 2025
Technology
0
15
正規表現エンジンを自作した話
北村
August 23, 2025
Tweet
Share
More Decks by 北村
See All by 北村
Azure Container Appsを使ってみた
shu_kita
0
53
ギャルのパンティおくれ
shu_kita
1
52
LSMツリー
shu_kita
0
18
掲示板への不適切な投稿を防ぐ
shu_kita
0
59
Other Decks in Technology
See All in Technology
松尾研LLM講座2025 応用編Day3「軽量化」 講義資料
aratako
0
350
AgentCoreとStrandsで社内d払いナレッジボットを作った話
motojimayu
1
720
MySQLとPostgreSQLのコレーション / Collation of MySQL and PostgreSQL
tmtms
1
1.1k
2025年 開発生産「可能」性向上報告 サイロ解消からチームが能動性を獲得するまで/ 20251216 Naoki Takahashi
shift_evolve
PRO
2
210
なぜ あなたはそんなに re:Invent に行くのか?
miu_crescent
PRO
0
130
たまに起きる外部サービスの障害に備えたり備えなかったりする話
egmc
0
380
ExpoのインダストリーブースでみたAWSが見せる製造業の未来
hamadakoji
0
190
事業の財務責任に向き合うリクルートデータプラットフォームのFinOps
recruitengineers
PRO
2
180
TED_modeki_共創ラボ_20251203.pdf
iotcomjpadmin
0
130
Amazon Bedrock Knowledge Bases × メタデータ活用で実現する検証可能な RAG 設計
tomoaki25
6
2.1k
AWS運用を効率化する!AWS Organizationsを軸にした一元管理の実践/nikkei-tech-talk-202512
nikkei_engineer_recruiting
0
160
1人1サービス開発しているチームでのClaudeCodeの使い方
noayaoshiro
2
570
Featured
See All Featured
A better future with KSS
kneath
240
18k
Embracing the Ebb and Flow
colly
88
4.9k
For a Future-Friendly Web
brad_frost
180
10k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5k
First, design no harm
axbom
PRO
1
1.1k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Tell your own story through comics
letsgokoyo
0
750
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Writing Fast Ruby
sferik
630
62k
Darren the Foodie - Storyboard
khoart
PRO
0
1.9k
A Soul's Torment
seathinner
1
2k
Transcript
VM型正規表現エンジンを自作した話 ~正規表現マッチングの仕組みをしろう~ shu-kitamura
自己紹介 • 1998年生まれ(26歳) • 大阪出身、4年前に札幌転勤 • テニス、フットサル • 今日は正規表現エンジンの仕組みについて話します •
LTは初めてです。お手柔らかに お手やわらカニ
1. 正規表現エンジンとは 正規表現のマッチングを行う処理系のこと。 主要な正規表現エンジンの実装 • DFA型 : 決定性有限オートマトン(DFA)を用いてマッチングを行う • VM型
: 正規表現用の命令セットを用いてマッチングを行う VMは Virtual Machine の略 パターン 例 : a(b|c)d 文字列 正規表現 エンジン マッチング結果 ※正規表現の構文の説明はしません
1. 正規表現エンジンとは 正規表現のマッチングを行う処理系のこと。 主要な正規表現エンジンの実装 • DFA型 : 決定性有限オートマトン(DFA)を用いてマッチングを行う • VM型
: 正規表現用の命令セットを用いてマッチングを行う VMは Virtual Machine の略 パターン 例 : a(b|c)d 文字列 正規表現 エンジン マッチング結果 ※正規表現の構文の説明はしません 今日はこっちの説明をします
2. マッチング処理の流れ VM型正規表現エンジンのマッチング処理の流れ ① パターンを解析して AST(抽象構文木)に変換 ② AST を命令に変換 ③
VM が命令を解釈して、文字列とマッチング パターン 例 : a(b|c)d 文字列 マッチング結果 VM型正規表現エンジン AST 命令 VM ① ② ③
2. マッチング処理の流れ ① パターンを解析して AST(抽象構文木) に変換する AST a b c
d or concat 解析 a(b|c)d b|c a d concat or a d concat b c 変換 パターン
2. マッチング処理の流れ ② AST を命令に変換 正規表現エンジンが使用する命令セット 命令 説明 char 文字の比較を行う
split 処理を分岐させる jump 指定した命令に移動する match マッチの成功を表す AST a b c d or concat 命令 0: char a 1 : split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 変換 仕組みを知る上では 「変換しているんだなぁ」という 理解でOKです。
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令 0: char a 1 :
split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 abd 命令と文字列を使って マッチング処理を行う 流れを説明します
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 abd a 次の命令/文字に進める 比較
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 b abd 2, 4の命令に分岐させる
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 b 2, 4の命令に分岐させる b b abd
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 b b abd 次の命令/文字に進める この分岐は終了 比較 比較
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 d abd 5の命令に移動させる
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 d abd 5の命令に移動させる d
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 abd d 次の命令/文字に進める 比較
③VMが命令を解釈して、文字列とマッチング 2. マッチング処理の流れ VM 命令(赤枠は処理中の命令 ) 0: char a 1
: split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match 文字列 abd match まで来たらマッチング成功
2. マッチング処理の流れ VM型正規表現エンジン AST a b c d or concat
パターン a(b|c)d 文字列 abd 命令 0: char a 1 : split 2, 4 2: char b 3: jump 5 4: char c 5: char d 6: match マッチング 成功 変換 変換 VM 命令を解釈して、 文字列とマッチング
し た ご清聴ありがとうございま