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
210
Azure Container Appsを使ってみた
shu_kita
0
150
ギャルのパンティおくれ
shu_kita
1
150
LSMツリー
shu_kita
0
21
掲示板への不適切な投稿を防ぐ
shu_kita
0
150
Other Decks in Technology
See All in Technology
顧客の言葉を、そのまま信じない勇気
yamatai1212
1
270
AI時代、1年目エンジニアの悩み
jin4
1
150
プロダクト成長を支える開発基盤とスケールに伴う課題
yuu26
2
940
Application Performance Optimisation in Practice (60 mins)
stevejgordon
0
110
Introduction to Bill One Development Engineer
sansan33
PRO
0
360
日本語テキストと音楽の対照学習の技術とその応用
lycorptech_jp
PRO
1
410
茨城の思い出を振り返る ~CDKのセキュリティを添えて~ / 20260201 Mitsutoshi Matsuo
shift_evolve
PRO
1
140
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1k
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
What happened to RubyGems and what can we learn?
mikemcquaid
0
210
2026年はチャンキングを極める!
shibuiwilliam
8
1.9k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
13k
Featured
See All Featured
Accessibility Awareness
sabderemane
0
47
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.5k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
760
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
Testing 201, or: Great Expectations
jmmastey
46
8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Typedesign – Prime Four
hannesfritz
42
2.9k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
30 Presentation Tips
portentint
PRO
1
210
Joys of Absence: A Defence of Solitary Play
codingconduct
1
280
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 命令を解釈して、 文字列とマッチング
し た ご清聴ありがとうございま