Upgrade to Pro — share decks privately, control downloads, hide ads and more …

More than Regexp

00e4a880b1262a125b5e342e4b536765?s=47 Zete
November 18, 2012

More than Regexp

RubyConfChina 2012 超越正则表达式的正则表达式

00e4a880b1262a125b5e342e4b536765?s=128

Zete

November 18, 2012
Tweet

More Decks by Zete

Other Decks in Programming

Transcript

  1. More Than Regexp https://github.com/luikore Monday, November 19, 12

  2. Who m i Monday, November 19, 12

  3. 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
  4. 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
  5. 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
  6. 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
  7. I am a rubyist just like you Monday, November 19,

    12
  8. on Languages Monday, November 19, 12

  9. Parsing Parsing is reading, the computer way Monday, November 19,

    12
  10. Regexp The built-in parsing tool of Ruby Monday, November 19,

    12
  11. ★ Search and replace Monday, November 19, 12

  12. ★ Search and replace ★ Validate form data Monday, November

    19, 12
  13. ★ Search and replace ★ Validate form data ★ Implement

    protocols Monday, November 19, 12
  14. ★ Search and replace ★ Validate form data ★ Implement

    protocols ★ Virus scan Monday, November 19, 12
  15. ★ Search and replace ★ Validate form data ★ Implement

    protocols ★ Virus scan ★ Matching mRNA motif Monday, November 19, 12
  16. ★ Search and replace ★ Validate form data ★ Implement

    protocols ★ Virus scan ★ Matching mRNA motif ★ ... Monday, November 19, 12
  17. A Brief History Monday, November 19, 12

  18. ★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p

    ★ Larry Wall, Perl Monday, November 19, 12
  19. ★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p

    ★ Larry Wall, Perl Monday, November 19, 12
  20. ★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p

    ★ Larry Wall, Perl Monday, November 19, 12
  21. ★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p

    ★ Larry Wall, Perl Monday, November 19, 12
  22. ★ Stephen Cole Kleene, 1950s ★ Ken Thompson, ed, g/re/p

    ★ Larry Wall, Perl Monday, November 19, 12
  23. Modern Implementations Goes far beyond the original definition Monday, November

    19, 12
  24. ★ Looks around Monday, November 19, 12

  25. ★ Looks around ★ Unicode support Monday, November 19, 12

  26. ★ Looks around ★ Unicode support ★ Matching history Monday,

    November 19, 12
  27. ★ Looks around ★ Unicode support ★ Matching history ★

    PEG engine in fact Monday, November 19, 12
  28. ★ Looks around ★ Unicode support ★ Matching history ★

    PEG engine in fact ★ Ruby 1.9 Oniguruma (َ⻋车) Monday, November 19, 12
  29. ★ Looks around ★ Unicode support ★ Matching history ★

    PEG engine in fact ★ Ruby 1.9 Oniguruma (َ⻋车) ★ Ruby 2.0 Onigmo (َӠ) Monday, November 19, 12
  30. Regexp in Ruby RUBY_VERSION =~ /1\.9|2\.0/ s = “肿㜮办?” s[/肿㜮/]

    = “ዎ㜮” #=> “ዎ㜮办?” %r(#{words.join ‘|’}) Monday, November 19, 12
  31. foo “foo” exactly Monday, November 19, 12

  32. foo “foo” exactly . matches ANY char Monday, November 19,

    12
  33. foo “foo” exactly . matches ANY char a|b or Monday,

    November 19, 12
  34. foo “foo” exactly . matches ANY char a|b or a?

    maybe yes, maybe no Monday, November 19, 12
  35. foo “foo” exactly . matches ANY char a|b or a?

    maybe yes, maybe no a* kleeeeeene star Monday, November 19, 12
  36. 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
  37. (a) group Monday, November 19, 12

  38. (a) group \1 back ref (fixed) Monday, November 19, 12

  39. (a) group \1 back ref (fixed) (?<name>a) define named group

    Monday, November 19, 12
  40. (a) group \1 back ref (fixed) (?<name>a) define named group

    \g<name> use named ref Monday, November 19, 12
  41. 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
  42. ★ Complex regexp contains much information Monday, November 19, 12

  43. ★ Complex regexp contains much information ★ Add space to

    make it human-readable Monday, November 19, 12
  44. ★ Complex regexp contains much information ★ Add space to

    make it human-readable ★ Try not to make too-complex regexps Monday, November 19, 12
  45. What does it do? /^[ \t]*(?:class)\s*(.*?) \s*(<.*?)?\s*(#.*)?$/ Monday, November 19,

    12
  46. Add margins and paddings /^ [\ \t]* (?:class)\s* (.*?)\s* (<.*?)?\s*

    (#.*)? $/x Monday, November 19, 12
  47. Alignment reduces visual complexity: /^ [\ \t]* (?:class) \s* (.*?)

    \s* (<.*?)? \s* (#.*)? $/x Monday, November 19, 12
  48. Add comments r = /^ [\ \t]* (?:class) \s* (.*?)

    \s* # class name (<.*?)? \s* # inheritance (#.*)? # line comment $/x r =~ “class A < B # match!” Monday, November 19, 12
  49. Mathematical modeling languages Formal Language Theory Monday, November 19, 12

  50. Mathematical modeling languages Formal Language Theory ਺ֶ Monday, November 19,

    12
  51. Mathematical modeling languages Formal Language Theory ਺ֶ 语จ Monday, November

    19, 12
  52. Regular Expression expresses Regular Grammar which recognizes Regular Language, which

    is Non-Recursive Monday, November 19, 12
  53. Parsing Expression Grammar recognizes parsing expression language, which can be

    Recursive Monday, November 19, 12
  54. Monday, November 19, 12

  55. Monday, November 19, 12

  56. Monday, November 19, 12

  57. Example -- Match the following strings: Monday, November 19, 12

  58. զ஌ಓ Example -- Match the following strings: Monday, November 19,

    12
  59. զ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ Example -- Match the following strings: Monday, November

    19, 12
  60. զ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ Example -- Match the following strings: Monday,

    November 19, 12
  61. զ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ Example -- Match the following strings:

    Monday, November 19, 12
  62. զ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ զ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ㟬஌ಓզ஌ಓ ... Example -- Match the following

    strings: Monday, November 19, 12
  63. The Language (A Regular Language): L = { զ஌ಓ, զ஌ಓ㟬஌ಓզ஌ಓ,

    ... } Monday, November 19, 12
  64. The Regexp (A Regular Grammar): /զ஌ಓ(㟬஌ಓզ஌ಓ)*/ Monday, November 19, 12

  65. Structural Analysis: զ஌ಓ(㟬஌ಓ( զ஌ಓ(զ஌ಓ( 㟬஌ಓ(զ஌ಓ) )) )) Monday, November 19,

    12
  66. Grammar in BNF Notation: <A> ::= “զ஌ಓ” <B>? <B> ::=

    “㟬஌ಓ” <A> Monday, November 19, 12
  67. Grammar in BNF Notation: <A> ::= “զ஌ಓ” <B>? <B> ::=

    “㟬஌ಓ” <A> Tail Recursion Monday, November 19, 12
  68. The “PEG-Flavored” Regexp: / (?<A> զ஌ಓ \g<B>? ){0} (?<B> 㟬஌ಓ

    \g<A> ){0} \g<A> /x Monday, November 19, 12
  69. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): Monday, November 19, 12

  70. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 Monday, November 19, 12

  71. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 谓语 Monday, November 19,

    12
  72. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ The really-recursive example (1): ओ语 宾语 谓语 Monday, November

    19, 12
  73. (?<ओ语>஖ᥨښ) (?<谓语>ੋ) (?<宾语>ষ⻥鱼ത࢜తၰࢠ) (?<陈ड़۟>\g<ओ语>\g<谓语>\g<宾语>) Monday, November 19, 12

  74. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ The really-recursive example (2): Monday, November 19, 12

  75. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ The really-recursive example (2): 陈ड़۟ Monday, November 19,

    12
  76. ஖ᥨښੋষ⻥鱼ത࢜తၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ The really-recursive example (2): 陈ड़۟ ݪҼဓ۟ Monday, November

    19, 12
  77. (?<ݪҼဓ۟>Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ) (?<ྫྷস话>\g<陈ड़۟>,\g<ݪҼဓ۟>) Monday, November 19, 12

  78. զࡏRubyConfChina্讲ྃྫྷস话: “஖ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ” The really-recursive example (3): Monday, November

    19, 12
  79. զࡏRubyConfChina্讲ྃྫྷস话: “஖ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ” The really-recursive example (3): 陈ड़۟ Monday,

    November 19, 12
  80. զࡏRubyConfChina্讲ྃྫྷস话: “஖ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ” The really-recursive example (3): 陈ड़۟ 宾语ಉҐ语

    Monday, November 19, 12
  81. (?<ओ语>஖ᥨښ|զ) (?<谓语>ੋ|ࡏRubyConfChina্讲ྃ) (?<宾语>ষ⻥鱼ത࢜తၰࢠ|ྫྷস话:\g<宾语ಉҐ语>) (?<宾语ಉҐ语>“\g<陈ड़۟>”) Monday, November 19, 12

  82. զࡏRubyConfChina্讲ྃྫྷস话: “஖ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ”,ୠେՈ຅༗স The really-recursive example (4): Monday, November

    19, 12
  83. զࡏRubyConfChina্讲ྃྫྷস话: “஖ᥨښੋষ⻥鱼ത࢜త ၰࢠ, Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ”,ୠେՈ຅༗স The really-recursive example (4): 转ંဓ۟ Monday,

    November 19, 12
  84. (?<转ંဓ۟>ୠେՈ຅༗স) (?<ྫྷস话>\g<陈ड़۟>, (\g<ݪҼဓ۟>|\g<转ંဓ۟>)) Monday, November 19, 12

  85. Combine them all Monday, November 19, 12

  86. / (?<ओ语> ஖ᥨښ|զ ){0} (?<谓语> ੋ|ࡏRubyConfChina্讲ྃ ){0} (?<宾语> ষ⻥鱼ത࢜తၰࢠ|ྫྷস话:\g<宾语ಉҐ语> ){0}

    (?<陈ड़۟> \g<ओ语>\g<谓语>\g<宾语> ){0} (?<ྫྷস话> \g<陈ड़۟>,(\g<ݪҼဓ۟>|\g<转ંဓ۟>) ){0} (?<ݪҼဓ۟> Ҽ为஖ᥨ࿨ষ⻥鱼౎༗ീ৚Ḱ ){0} (?<宾语ಉҐ语> “\g<ྫྷস话>” ){0} (?<转ંဓ۟> ୠେՈ຅༗স ){0} \g<ྫྷস话> /x Monday, November 19, 12
  87. Use dictionaries in the sentence components, you can make a

    natural language parser with “Regexp” (PEG in fact) Monday, November 19, 12
  88. Use dictionaries in the sentence components, you can make a

    natural language parser with “Regexp” (PEG in fact) (?<ओ语>஖ᥨښ|᥸㫴ښ|౮笼ښ|...) Monday, November 19, 12
  89. Real world language is a bit more than PEG, generally

    Context Free Grammar. Monday, November 19, 12
  90. 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
  91. Even CFG can’t solve some ambiguity Monday, November 19, 12

  92. Even CFG can’t solve some ambiguity ༗Ұେ೾ၣረਖ਼ࡏ઀ۙ Monday, November 19,

    12
  93. Even CFG can’t solve some ambiguity ༗Ұେ೾ၣረਖ਼ࡏ઀ۙ ( ) ??

    Monday, November 19, 12
  94. Now you know more than regexp parsec, rsec, treetop, parselet

    ... Monday, November 19, 12
  95. Simple markdown parser in 130 lines (many features ignored but...)

    Real world example Monday, November 19, 12
  96. 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
  97. The parser for nested parens: /(?<paren> \( ( [^\(\)]+ #

    non-paren chars | # or \g<paren> # a paren )* \) )/x Monday, November 19, 12
  98. ★ 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
  99. More sugars with Onigmo If there’s time... Monday, November 19,

    12
  100. /\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
  101. /(?<=backward)something(?=forward)/ /(?<!backward)something(?!forward)/ /(?<\b)hello/ # 1.9 :( # 2.0 :) Monday,

    November 19, 12
  102. ? + * +? *? ?+ ++ *+ Can you

    tell all of them? Monday, November 19, 12
  103. Thanks Monday, November 19, 12