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
25
0
Share
正規表現エンジンを自作した話
北村
August 23, 2025
More Decks by 北村
See All by 北村
LLMの出力を構造化したい
shu_kita
0
320
Azure Container Appsを使ってみた
shu_kita
0
240
ギャルのパンティおくれ
shu_kita
1
230
LSMツリー
shu_kita
0
25
掲示板への不適切な投稿を防ぐ
shu_kita
0
240
Other Decks in Technology
See All in Technology
Purview Endpoint DLP 動かしてみた
kozakigh
1
450
開発サイクルのボーダーレス化に伴う組織変革から学んだこと / Organizational Transformation Amid the Borderless Development Cycle
mii3king
0
290
クラウドネイティブ DB はいかにして制約を 克服したか? 〜進化歴史から紐解く、スケーラブルアーキテクチャ設計指針〜
hacomono
PRO
6
1.1k
DI コンテナ自動生成ツールを実装してみた / intro-autodi
uhzz
0
440
そのSLO 99.9%、本当に必要ですか? 〜優先度付きSLOによる責任共有の設計思想〜 / Is that 99.9% SLO really necessary? Design philosophy of shared responsibility through prioritized SLOs
vtryo
0
840
PdM・Eng・QAで進めるAI駆動開発の現在地/aidd-with-pdm-eng-qa
shota_kusaba
0
260
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
4.5k
実例から学ぶ GuardDuty(SSH BruteForce)調査の全体フローと勘所【SecurityJAWS】
cscengineer
PRO
0
140
Cortex(Code) を ML モデルの 精度改善サイクルに組み込む.pdf
oimo23
0
230
JTCでRedmine利用者2700人を実現した手法 第二部
nobuonakamura
0
140
Oracle AI Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
6
1.4k
既存プロダクトQAから新規プロダクトQAへ
ryotakahashi
0
160
Featured
See All Featured
Amusing Abliteration
ianozsvald
1
170
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
150
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
110
[SF Ruby Conf 2025] Rails X
palkan
2
1k
The browser strikes back
jonoalderson
0
1.1k
Between Models and Reality
mayunak
4
290
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
200
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.6k
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 命令を解釈して、 文字列とマッチング
し た ご清聴ありがとうございま