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
正規表現エンジンを自作した話
Search
北村
August 23, 2025
Technology
0
17
正規表現エンジンを自作した話
北村
August 23, 2025
Tweet
Share
More Decks by 北村
See All by 北村
LLMの出力を構造化したい
shu_kita
0
250
Azure Container Appsを使ってみた
shu_kita
0
180
ギャルのパンティおくれ
shu_kita
1
170
LSMツリー
shu_kita
0
21
掲示板への不適切な投稿を防ぐ
shu_kita
0
180
Other Decks in Technology
See All in Technology
(技術的には)社内システムもOKなブラウザエージェントを作ってみた!
har1101
0
330
プロダクト成長を支える開発基盤とスケールに伴う課題
yuu26
4
1.4k
登壇駆動学習のすすめ — CfPのネタの見つけ方と書くときに意識していること
bicstone
3
130
制約が導く迷わない設計 〜 信頼性と運用性を両立するマイナンバー管理システムの実践 〜
bwkw
3
1.1k
usermode linux without MMU - fosdem2026 kernel devroom
thehajime
0
240
CDKで始めるTypeScript開発のススメ
tsukuboshi
1
570
22nd ACRi Webinar - NTT Kawahara-san's slide
nao_sumikawa
0
110
AIが実装する時代、人間は仕様と検証を設計する
gotalab555
1
600
10Xにおける品質保証活動の全体像と改善 #no_more_wait_for_test
nihonbuson
PRO
2
340
SREのプラクティスを用いた3領域同時 マネジメントへの挑戦 〜SRE・情シス・セキュリティを統合した チーム運営術〜
coconala_engineer
2
780
Exadata Fleet Update
oracle4engineer
PRO
0
1.1k
GitHub Issue Templates + Coding Agentで簡単みんなでIaC/Easy IaC for Everyone with GitHub Issue Templates + Coding Agent
aeonpeople
1
260
Featured
See All Featured
Designing Powerful Visuals for Engaging Learning
tmiket
0
240
The agentic SEO stack - context over prompts
schlessera
0
650
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.2k
Building the Perfect Custom Keyboard
takai
2
690
sira's awesome portfolio website redesign presentation
elsirapls
0
150
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
Git: the NoSQL Database
bkeepers
PRO
432
66k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
100
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
430
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 命令を解釈して、 文字列とマッチング
し た ご清聴ありがとうございま