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
More than Regexp
Search
Zete
November 18, 2012
Programming
16
1.2k
More than Regexp
RubyConfChina 2012 超越正则表达式的正则表达式
Zete
November 18, 2012
Tweet
Share
More Decks by Zete
See All by Zete
Thread Mist
luikore
7
720
Intro to Rust Programming Language
luikore
2
210
GC in Ruby 2.2
luikore
0
160
Building an Async Server with Fiber
luikore
3
730
Ruby 2.1 Walk Thru (title bait)
luikore
14
7.7k
Other Decks in Programming
See All in Programming
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
RubyLSPのマルチバイト文字対応
notfounds
0
120
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Amazon Qを使ってIaCを触ろう!
maruto
0
400
Arm移行タイムアタック
qnighy
0
320
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
Scaling GitHub
holman
458
140k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Speed Design
sergeychernyshev
25
620
It's Worth the Effort
3n
183
27k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Adopting Sorbet at Scale
ufuk
73
9.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Six Lessons from altMBA
skipperchong
27
3.5k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Transcript
More Than Regexp https://github.com/luikore Monday, November 19, 12
Who m i Monday, November 19, 12
I mastered Basic, C, C++, Objective-C, Java, C#, Mathematica, Ruby,
Perl, Python, CoffeeScript, Haskell, Scala, Groovy, R, SML, Erlang, F#, MASM / GNU assembly, LLVM assembly Monday, November 19, 12
I mastered Basic, C, C++, Objective-C, Java, C#, Mathematica, Ruby,
Perl, Python, CoffeeScript, Haskell, Scala, Groovy, R, SML, Erlang, F#, MASM / GNU assembly, LLVM assembly ’s hello world Monday, November 19, 12
I know quite a lot about web development technologies, compiler
techniques, functional programing, quantum mechanics, abstract algebra, category theory and medieval history Monday, November 19, 12
I know quite a lot about web development technologies, compiler
techniques, functional programing, quantum mechanics, abstract algebra, category theory and medieval history a little bit Monday, November 19, 12
I am a rubyist just like you Monday, November 19,
12
on Languages Monday, November 19, 12
Parsing Parsing is reading, the computer way Monday, November 19,
12
Regexp The built-in parsing tool of Ruby Monday, November 19,
12
★ Search and replace Monday, November 19, 12
★ Search and replace ★ Validate form data Monday, November
19, 12
★ Search and replace ★ Validate form data ★ Implement
protocols Monday, November 19, 12
★ Search and replace ★ Validate form data ★ Implement
protocols ★ Virus scan Monday, November 19, 12
★ Search and replace ★ Validate form data ★ Implement
protocols ★ Virus scan ★ Matching mRNA motif Monday, November 19, 12
★ Search and replace ★ Validate form data ★ Implement
protocols ★ Virus scan ★ Matching mRNA motif ★ ... Monday, November 19, 12
A Brief History Monday, November 19, 12
★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p
★ Larry Wall, Perl Monday, November 19, 12
★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p
★ Larry Wall, Perl Monday, November 19, 12
★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p
★ Larry Wall, Perl Monday, November 19, 12
★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p
★ Larry Wall, Perl Monday, November 19, 12
★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p
★ Larry Wall, Perl Monday, November 19, 12
Modern Implementations Goes far beyond the original definition Monday, November
19, 12
★ Looks around Monday, November 19, 12
★ Looks around ★ Unicode support Monday, November 19, 12
★ Looks around ★ Unicode support ★ Matching history Monday,
November 19, 12
★ Looks around ★ Unicode support ★ Matching history ★
PEG engine in fact Monday, November 19, 12
★ Looks around ★ Unicode support ★ Matching history ★
PEG engine in fact ★ Ruby 1.9 Oniguruma (َ⻋车) Monday, November 19, 12
★ Looks around ★ Unicode support ★ Matching history ★
PEG engine in fact ★ Ruby 1.9 Oniguruma (َ⻋车) ★ Ruby 2.0 Onigmo (َӠ) Monday, November 19, 12
Regexp in Ruby RUBY_VERSION =~ /1\.9|2\.0/ s = “肿㜮办?” s[/肿㜮/]
= “ዎ㜮” #=> “ዎ㜮办?” %r(#{words.join ‘|’}) Monday, November 19, 12
foo “foo” exactly Monday, November 19, 12
foo “foo” exactly . matches ANY char Monday, November 19,
12
foo “foo” exactly . matches ANY char a|b or Monday,
November 19, 12
foo “foo” exactly . matches ANY char a|b or a?
maybe yes, maybe no Monday, November 19, 12
foo “foo” exactly . matches ANY char a|b or a?
maybe yes, maybe no a* kleeeeeene star Monday, November 19, 12
foo “foo” exactly . matches ANY char a|b or a?
maybe yes, maybe no a* kleeeeeene star a{0} repeat by 0 times Monday, November 19, 12
(a) group Monday, November 19, 12
(a) group \1 back ref (fixed) Monday, November 19, 12
(a) group \1 back ref (fixed) (?<name>a) define named group
Monday, November 19, 12
(a) group \1 back ref (fixed) (?<name>a) define named group
\g<name> use named ref Monday, November 19, 12
Difference between back ref and named group backref = /(\w+)
\1/ backref =~ ‘ha ha’ # 0 backref =~ ‘ha ho’ # false named = /(?:<word>\w+) \g<word>/ named =~ ‘ha ha’ # 0 named =~ ‘ha ho’ # 0 Monday, November 19, 12
★ Complex regexp contains much information Monday, November 19, 12
★ Complex regexp contains much information ★ Add space to
make it human-readable Monday, November 19, 12
★ Complex regexp contains much information ★ Add space to
make it human-readable ★ Try not to make too-complex regexps Monday, November 19, 12
What does it do? /^[ \t]*(?:class)\s*(.*?) \s*(<.*?)?\s*(#.*)?$/ Monday, November 19,
12
Add margins and paddings /^ [\ \t]* (?:class)\s* (.*?)\s* (<.*?)?\s*
(#.*)? $/x Monday, November 19, 12
Alignment reduces visual complexity: /^ [\ \t]* (?:class) \s* (.*?)
\s* (<.*?)? \s* (#.*)? $/x Monday, November 19, 12
Add comments r = /^ [\ \t]* (?:class) \s* (.*?)
\s* # class name (<.*?)? \s* # inheritance (#.*)? # line comment $/x r =~ “class A < B # match!” Monday, November 19, 12
Mathematical modeling languages Formal Language Theory Monday, November 19, 12
Mathematical modeling languages Formal Language Theory ֶ Monday, November 19,
12
Mathematical modeling languages Formal Language Theory ֶ 语จ Monday, November
19, 12
Regular Expression expresses Regular Grammar which recognizes Regular Language, which
is Non-Recursive Monday, November 19, 12
Parsing Expression Grammar recognizes parsing expression language, which can be
Recursive Monday, November 19, 12
Monday, November 19, 12
Monday, November 19, 12
Monday, November 19, 12
Example -- Match the following strings: Monday, November 19, 12
զಓ Example -- Match the following strings: Monday, November 19,
12
զಓ զಓ㟬ಓզಓ Example -- Match the following strings: Monday, November
19, 12
զಓ զಓ㟬ಓզಓ զಓ㟬ಓզಓ㟬ಓզಓ Example -- Match the following strings: Monday,
November 19, 12
զಓ զಓ㟬ಓզಓ զಓ㟬ಓզಓ㟬ಓզಓ զಓ㟬ಓզಓ㟬ಓզಓ㟬ಓզಓ Example -- Match the following strings:
Monday, November 19, 12
զಓ զಓ㟬ಓզಓ զಓ㟬ಓզಓ㟬ಓզಓ զಓ㟬ಓզಓ㟬ಓզಓ㟬ಓզಓ ... Example -- Match the following
strings: Monday, November 19, 12
The Language (A Regular Language): L = { զಓ, զಓ㟬ಓզಓ,
... } Monday, November 19, 12
The Regexp (A Regular Grammar): /զಓ(㟬ಓզಓ)*/ Monday, November 19, 12
Structural Analysis: զಓ(㟬ಓ( զಓ(զಓ( 㟬ಓ(զಓ) )) )) Monday, November 19,
12
Grammar in BNF Notation: <A> ::= “զಓ” <B>? <B> ::=
“㟬ಓ” <A> Monday, November 19, 12
Grammar in BNF Notation: <A> ::= “զಓ” <B>? <B> ::=
“㟬ಓ” <A> Tail Recursion Monday, November 19, 12
The “PEG-Flavored” Regexp: / (?<A> զಓ \g<B>? ){0} (?<B> 㟬ಓ
\g<A> ){0} \g<A> /x Monday, November 19, 12
ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): Monday, November 19, 12
ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 Monday, November 19, 12
ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 谓语 Monday, November 19,
12
ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 宾语 谓语 Monday, November
19, 12
(?<ओ语>ᥨښ) (?<谓语>ੋ) (?<宾语>ষ⻥鱼ത࢜తၰࢠ) (?<陈ड़۟>\g<ओ语>\g<谓语>\g<宾语>) Monday, November 19, 12
ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ The really-recursive example (2): Monday, November 19, 12
ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ The really-recursive example (2): 陈ड़۟ Monday, November 19,
12
ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ The really-recursive example (2): 陈ड़۟ ݪҼဓ۟ Monday, November
19, 12
(?<ݪҼဓ۟>Ҽ为ᥨষ⻥鱼༗ീḰ) (?<ྫྷস话>\g<陈ड़۟>,\g<ݪҼဓ۟>) Monday, November 19, 12
զࡏRubyConfChina্讲ྃྫྷস话: “ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ” The really-recursive example (3): Monday, November
19, 12
զࡏRubyConfChina্讲ྃྫྷস话: “ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ” The really-recursive example (3): 陈ड़۟ Monday,
November 19, 12
զࡏRubyConfChina্讲ྃྫྷস话: “ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ” The really-recursive example (3): 陈ड़۟ 宾语ಉҐ语
Monday, November 19, 12
(?<ओ语>ᥨښ|զ) (?<谓语>ੋ|ࡏRubyConfChina্讲ྃ) (?<宾语>ষ⻥鱼ത࢜తၰࢠ|ྫྷস话:\g<宾语ಉҐ语>) (?<宾语ಉҐ语>“\g<陈ड़۟>”) Monday, November 19, 12
զࡏRubyConfChina্讲ྃྫྷস话: “ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ”,ୠେՈ༗স The really-recursive example (4): Monday, November
19, 12
զࡏRubyConfChina্讲ྃྫྷস话: “ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为ᥨষ⻥鱼༗ീḰ”,ୠେՈ༗স The really-recursive example (4): 转ંဓ۟ Monday,
November 19, 12
(?<转ંဓ۟>ୠେՈ༗স) (?<ྫྷস话>\g<陈ड़۟>, (\g<ݪҼဓ۟>|\g<转ંဓ۟>)) Monday, November 19, 12
Combine them all Monday, November 19, 12
/ (?<ओ语> ᥨښ|զ ){0} (?<谓语> ੋ|ࡏRubyConfChina্讲ྃ ){0} (?<宾语> ষ⻥鱼ത࢜తၰࢠ|ྫྷস话:\g<宾语ಉҐ语> ){0}
(?<陈ड़۟> \g<ओ语>\g<谓语>\g<宾语> ){0} (?<ྫྷস话> \g<陈ड़۟>,(\g<ݪҼဓ۟>|\g<转ંဓ۟>) ){0} (?<ݪҼဓ۟> Ҽ为ᥨষ⻥鱼༗ീḰ ){0} (?<宾语ಉҐ语> “\g<ྫྷস话>” ){0} (?<转ંဓ۟> ୠେՈ༗স ){0} \g<ྫྷস话> /x Monday, November 19, 12
Use dictionaries in the sentence components, you can make a
natural language parser with “Regexp” (PEG in fact) Monday, November 19, 12
Use dictionaries in the sentence components, you can make a
natural language parser with “Regexp” (PEG in fact) (?<ओ语>ᥨښ|㫴ښ|౮笼ښ|...) Monday, November 19, 12
Real world language is a bit more than PEG, generally
Context Free Grammar. Monday, November 19, 12
In CFG, the branches are not ordered: Assume A and
B are two rules, A|B is the same as B|A in CFG. Monday, November 19, 12
Even CFG can’t solve some ambiguity Monday, November 19, 12
Even CFG can’t solve some ambiguity ༗Ұେၣረਖ਼ࡏۙ Monday, November 19,
12
Even CFG can’t solve some ambiguity ༗Ұେၣረਖ਼ࡏۙ ( ) ??
Monday, November 19, 12
Now you know more than regexp parsec, rsec, treetop, parselet
... Monday, November 19, 12
Simple markdown parser in 130 lines (many features ignored but...)
Real world example Monday, November 19, 12
It supports nested parens! (while ruby-china doesn’t) [ruby](http://en.wikipedia.org/wiki/Ruby_(programming_language)) <a href=’
http://en.wikipedia.org/wiki/Ruby_(programming_language)’> ruby </a> Monday, November 19, 12
The parser for nested parens: /(?<paren> \( ( [^\(\)]+ #
non-paren chars | # or \g<paren> # a paren )* \) )/x Monday, November 19, 12
★ ri Regexp ★ https://github.com/k-takata/Onigmo/tree/master/doc/RE ★ http://en.wikipedia.org/wiki/Parsing_Expression_Grammar Helpful Links Monday,
November 19, 12
More sugars with Onigmo If there’s time... Monday, November 19,
12
/\p{Han}/ /\p{Hiragana,Katakana}/ /\g'name'/ /\g<3>/ /\g'-3'/ /\k<1>/ # /\1/ /\k'name'/ /\k<-1>/
/\k'-n-level'/ Monday, November 19, 12
/(?<=backward)something(?=forward)/ /(?<!backward)something(?!forward)/ /(?<\b)hello/ # 1.9 :( # 2.0 :) Monday,
November 19, 12
? + * +? *? ?+ ++ *+ Can you
tell all of them? Monday, November 19, 12
Thanks Monday, November 19, 12