SlideShare a Scribd company logo
1 of 13
Download to read offline
Regexp.new('2.0.0')
Ruby 2.0.0 での正規表現の新機能
西山和広
日本Rubyの会
Powered by Rabbit 2.0.8
Onigmo
Onigmo (Oniguruma-mod)
NEWS of Ruby 2.0.0 says following only:
Merge Onigmo
https://github.com/k-takata/Onigmo
Details are unknown
詳細不明
1/12
New feature (1) K
examples without /K/
"foobar".sub(/(?<=foo)bar/, "") #=> "foo"
"foobar".sub(/(?<=fo*)bar/, "")
# SyntaxError: invalid pattern in look-behind: /(?<=fo*)bar/
examples with /K/
"foobar".sub(/fooKbar/, "") #=> "foo"
"foobar".sub(/fo*Kbar/, "") #=> "foo"
2/12
New feature (1) K
Treat the first non-blank character of the line.
examples with /K/
gsub(/^ *K(d+)/) { $1.to_i+1 }
examples without /K/
gsub(/^( *)(d+)/) { "#{$1}#{$2.to_i+1}" }
3/12
New feature (2) R
Linebreak
改行文字
Unicode:
(?>x0Dx0A|[x0A-x0Dx{85}x{2028}x{2029}])
Not Unicode:
(?>x0Dx0A|[x0A-x0D])
4/12
New feature (3) X
eXtended grapheme cluster
拡張書記素クラスタ
Unicode:
(?>P{M}p{M}*)
Not Unicode:
(?m:.)
5/12
Extended grapheme cluster
example:
"u{304B 3099}"[/X/].size #=> 2
U+304B HIRAGANA LETTER KA
U+3099 COMBINING KATAKANA-HIRAGANA
VOICED SOUND MARK
see [UAX #29] for more detail
(Unicode標準附属書29)
6/12
New feature (4)
conditional expression:
(?(cond)yes)
(?(cond)yes|no)
example:
" :f o o "[/:(['"])?(?(1)[ws]+1|w+)/]
#=> ":f"
":'f o o'"[/:(['"])?(?(1)[ws]+1|w+)/]
#=> ":'f o o'"
7/12
(?adu)
character set option (character range
option)
文字集合オプション (文字範囲オプション)
d: Default (compatible with Ruby 1.9.3)
a: ASCII
u: Unicode
see doc/RE in Onigmo for more detail
8/12
(?adu)
examples:
"u{3042}"[/w/] #=> nil
"u{3042}"[/(?a)w/] #=> nil
"u{3042}"[/(?d)w/] #=> nil
"u{3042}"[/(?u)w/] #=> "あ"
/ab/ =~ "au{3042}" #=> nil
/(?a)ab/ =~ "au{3042}" #=> 0
/(?d)ab/ =~ "au{3042}" #=> nil
/(?u)ab/ =~ "au{3042}" #=> nil
9/12
(?adu)
(?-a), (?-d), (?-u) do not found
unlike (?-i), (?-m), (?-x)
10/12
Character Property
support for Unicode blocks
example:
/p{InHiragana}/ =~ "u3042" #=> 0
/p{InCJKUnifiedIdeographs}/ =~ "u3042" #=> nil
see tool/enc-unicode.rb in Onigmo for
more detail
11/12
/z/

More Related Content

What's hot

2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)charsbar
 
Composer - Package Management for PHP. Silver Bullet?
Composer - Package Management for PHP. Silver Bullet?Composer - Package Management for PHP. Silver Bullet?
Composer - Package Management for PHP. Silver Bullet?Kirill Chebunin
 
Ruby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelRuby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelJiang Wu
 
関西アンカンファレンス PHP ではじめるテストコード
関西アンカンファレンス PHP ではじめるテストコード関西アンカンファレンス PHP ではじめるテストコード
関西アンカンファレンス PHP ではじめるテストコードShinya Ohyanagi
 
Assurer - a pluggable server testing/monitoring framework
Assurer - a pluggable server testing/monitoring frameworkAssurer - a pluggable server testing/monitoring framework
Assurer - a pluggable server testing/monitoring frameworkGosuke Miyashita
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilitiesnagachika t
 
Better detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 codeBetter detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 codecharsbar
 
plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6Nobuo Danjou
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Romain Dorgueil
 
2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL TigerAkihiro Okuno
 
Information security programming in ruby
Information security programming in rubyInformation security programming in ruby
Information security programming in rubyHiroshi Nakamura
 
Laravel でやってみるクリーンアーキテクチャ #phpconfuk
Laravel でやってみるクリーンアーキテクチャ #phpconfukLaravel でやってみるクリーンアーキテクチャ #phpconfuk
Laravel でやってみるクリーンアーキテクチャ #phpconfukShohei Okada
 
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyoエラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyoShohei Okada
 

What's hot (20)

2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)
 
Composer - Package Management for PHP. Silver Bullet?
Composer - Package Management for PHP. Silver Bullet?Composer - Package Management for PHP. Silver Bullet?
Composer - Package Management for PHP. Silver Bullet?
 
Ruby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelRuby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequel
 
Ruby HTTP clients
Ruby HTTP clientsRuby HTTP clients
Ruby HTTP clients
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
関西アンカンファレンス PHP ではじめるテストコード
関西アンカンファレンス PHP ではじめるテストコード関西アンカンファレンス PHP ではじめるテストコード
関西アンカンファレンス PHP ではじめるテストコード
 
Assurer - a pluggable server testing/monitoring framework
Assurer - a pluggable server testing/monitoring frameworkAssurer - a pluggable server testing/monitoring framework
Assurer - a pluggable server testing/monitoring framework
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilities
 
Better detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 codeBetter detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 code
 
plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
 
node ffi
node ffinode ffi
node ffi
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
 
Plack - LPW 2009
Plack - LPW 2009Plack - LPW 2009
Plack - LPW 2009
 
2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger
 
Information security programming in ruby
Information security programming in rubyInformation security programming in ruby
Information security programming in ruby
 
Laravel でやってみるクリーンアーキテクチャ #phpconfuk
Laravel でやってみるクリーンアーキテクチャ #phpconfukLaravel でやってみるクリーンアーキテクチャ #phpconfuk
Laravel でやってみるクリーンアーキテクチャ #phpconfuk
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyoエラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
 

More from Kazuhiro Nishiyama

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたKazuhiro Nishiyama
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話Kazuhiro Nishiyama
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Kazuhiro Nishiyama
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdfKazuhiro Nishiyama
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfKazuhiro Nishiyama
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたKazuhiro Nishiyama
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるKazuhiro Nishiyama
 
あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能Kazuhiro Nishiyama
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Kazuhiro Nishiyama
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメKazuhiro Nishiyama
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたKazuhiro Nishiyama
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法Kazuhiro Nishiyama
 

More from Kazuhiro Nishiyama (20)

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げた
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdf
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみた
 
systemd 再入門
systemd 再入門systemd 再入門
systemd 再入門
 
Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集
 
livedoor天気API終了対応
livedoor天気API終了対応livedoor天気API終了対応
livedoor天気API終了対応
 
Wireguard 実践入門
Wireguard 実践入門Wireguard 実践入門
Wireguard 実践入門
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考える
 
あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメ
 
Dokku の紹介
Dokku の紹介Dokku の紹介
Dokku の紹介
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみた
 
Ruby svn to git
Ruby svn to gitRuby svn to git
Ruby svn to git
 
Ruby 2.6 Update
Ruby 2.6 UpdateRuby 2.6 Update
Ruby 2.6 Update
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法
 
Language update 2018 - ruby
Language update 2018 - rubyLanguage update 2018 - ruby
Language update 2018 - ruby
 

Ruby 2.0.0 正規表現の新機能

  • 2. Onigmo Onigmo (Oniguruma-mod) NEWS of Ruby 2.0.0 says following only: Merge Onigmo https://github.com/k-takata/Onigmo Details are unknown 詳細不明 1/12
  • 3. New feature (1) K examples without /K/ "foobar".sub(/(?<=foo)bar/, "") #=> "foo" "foobar".sub(/(?<=fo*)bar/, "") # SyntaxError: invalid pattern in look-behind: /(?<=fo*)bar/ examples with /K/ "foobar".sub(/fooKbar/, "") #=> "foo" "foobar".sub(/fo*Kbar/, "") #=> "foo" 2/12
  • 4. New feature (1) K Treat the first non-blank character of the line. examples with /K/ gsub(/^ *K(d+)/) { $1.to_i+1 } examples without /K/ gsub(/^( *)(d+)/) { "#{$1}#{$2.to_i+1}" } 3/12
  • 5. New feature (2) R Linebreak 改行文字 Unicode: (?>x0Dx0A|[x0A-x0Dx{85}x{2028}x{2029}]) Not Unicode: (?>x0Dx0A|[x0A-x0D]) 4/12
  • 6. New feature (3) X eXtended grapheme cluster 拡張書記素クラスタ Unicode: (?>P{M}p{M}*) Not Unicode: (?m:.) 5/12
  • 7. Extended grapheme cluster example: "u{304B 3099}"[/X/].size #=> 2 U+304B HIRAGANA LETTER KA U+3099 COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK see [UAX #29] for more detail (Unicode標準附属書29) 6/12
  • 8. New feature (4) conditional expression: (?(cond)yes) (?(cond)yes|no) example: " :f o o "[/:(['"])?(?(1)[ws]+1|w+)/] #=> ":f" ":'f o o'"[/:(['"])?(?(1)[ws]+1|w+)/] #=> ":'f o o'" 7/12
  • 9. (?adu) character set option (character range option) 文字集合オプション (文字範囲オプション) d: Default (compatible with Ruby 1.9.3) a: ASCII u: Unicode see doc/RE in Onigmo for more detail 8/12
  • 10. (?adu) examples: "u{3042}"[/w/] #=> nil "u{3042}"[/(?a)w/] #=> nil "u{3042}"[/(?d)w/] #=> nil "u{3042}"[/(?u)w/] #=> "あ" /ab/ =~ "au{3042}" #=> nil /(?a)ab/ =~ "au{3042}" #=> 0 /(?d)ab/ =~ "au{3042}" #=> nil /(?u)ab/ =~ "au{3042}" #=> nil 9/12
  • 11. (?adu) (?-a), (?-d), (?-u) do not found unlike (?-i), (?-m), (?-x) 10/12
  • 12. Character Property support for Unicode blocks example: /p{InHiragana}/ =~ "u3042" #=> 0 /p{InCJKUnifiedIdeographs}/ =~ "u3042" #=> nil see tool/enc-unicode.rb in Onigmo for more detail 11/12
  • 13. /z/