From 92c919befb55234c77263b9198fd701e9b356e90 Mon Sep 17 00:00:00 2001 From: "v.tikhonov" Date: Tue, 17 Mar 2015 16:55:36 +0300 Subject: [PATCH 1/3] Smiles matcher --- README.md | 10 ++++++++++ lib/regexy.rb | 5 +++-- lib/regexy/text.rb | 5 +++++ lib/regexy/text/smile.rb | 11 +++++++++++ spec/text/smile_spec.rb | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 lib/regexy/text.rb create mode 100644 lib/regexy/text/smile.rb create mode 100644 spec/text/smile_spec.rb diff --git a/README.md b/README.md index 0b0b858..54afd7c 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Regexy is the ruby gem that contains a lot of common-use regular expressions (su * [IP addresses](#regexywebipv4) * [Url](#regexyweburl) * [Hostname](#regexywebhostname) + * [Smiles](#regexytextsmile) - [Contributing](#contributing) ## Installation @@ -137,6 +138,15 @@ Generates regular expressions for matching hostname (with unicode support). ```ruby r1 = Regexy::Web::HostName.new # matches 'foo.com', 'www.foo.com' and 'киррилический.домен.рф' ``` +### Regexy::Text::Smile + +Generates regular expressions for matching smiles. +```ruby +r = Regexy::Text::Smile.new # matches ':)', ':=)', 'xD' and so on +# Find all smiles in text +str = "Check out http://foo.com :). It's awesome :D" +str.scan(r.unbound.internal_regexp).map(&:first) # => [":)", ":D"] +``` ## Contributing Have an idea of new regular expression? Create an [issue](https://github.com/vladimir-tikhonov/regexy/issues) (some test cases will be much appreciated) or open a [pull request](https://github.com/vladimir-tikhonov/regexy/pulls). diff --git a/lib/regexy.rb b/lib/regexy.rb index c765ad1..8008d6e 100644 --- a/lib/regexy.rb +++ b/lib/regexy.rb @@ -1,7 +1,8 @@ require 'regexy/version' module Regexy - autoload :Regexp, 'regexy/regexp' + autoload :Regexp, 'regexy/regexp' autoload :RegexpWithMode, 'regexy/regexp' - autoload :Web, 'regexy/web' + autoload :Web, 'regexy/web' + autoload :Text, 'regexy/text' end diff --git a/lib/regexy/text.rb b/lib/regexy/text.rb new file mode 100644 index 0000000..1cbb170 --- /dev/null +++ b/lib/regexy/text.rb @@ -0,0 +1,5 @@ +module Regexy + module Text + autoload :Smile, 'regexy/text/smile' + end +end \ No newline at end of file diff --git a/lib/regexy/text/smile.rb b/lib/regexy/text/smile.rb new file mode 100644 index 0000000..16c7236 --- /dev/null +++ b/lib/regexy/text/smile.rb @@ -0,0 +1,11 @@ +module Regexy + module Text + class Smile < Regexy::Regexp + SMILE_REGEX = /\A((? Date: Tue, 17 Mar 2015 17:14:59 +0300 Subject: [PATCH 2/3] Emojis matcher --- README.md | 11 +++++++++++ lib/regexy/text.rb | 1 + lib/regexy/text/emoji.rb | 13 +++++++++++++ spec/text/emoji_spec.rb | 11 +++++++++++ 4 files changed, 36 insertions(+) create mode 100644 lib/regexy/text/emoji.rb create mode 100644 spec/text/emoji_spec.rb diff --git a/README.md b/README.md index 54afd7c..9acfd7b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Regexy is the ruby gem that contains a lot of common-use regular expressions (su * [Url](#regexyweburl) * [Hostname](#regexywebhostname) * [Smiles](#regexytextsmile) + * [Emojis](#regexytextemoji) - [Contributing](#contributing) ## Installation @@ -148,5 +149,15 @@ str = "Check out http://foo.com :). It's awesome :D" str.scan(r.unbound.internal_regexp).map(&:first) # => [":)", ":D"] ``` +### Regexy::Text::Emoji + +Generates regular expressions for matching emojis. +```ruby +r = Regexy::Text::Emoji.new # matches '😀','😄' and so on +# Replace all emojis with 'x_x' +str = "Check out http://foo.com 😀. It's awesome 😼" +str.gsub(r.internal_regexp, 'x_x') # => "Check out http://foo.com x_x. It's awesome x_x" +``` + ## Contributing Have an idea of new regular expression? Create an [issue](https://github.com/vladimir-tikhonov/regexy/issues) (some test cases will be much appreciated) or open a [pull request](https://github.com/vladimir-tikhonov/regexy/pulls). diff --git a/lib/regexy/text.rb b/lib/regexy/text.rb index 1cbb170..652ade8 100644 --- a/lib/regexy/text.rb +++ b/lib/regexy/text.rb @@ -1,5 +1,6 @@ module Regexy module Text autoload :Smile, 'regexy/text/smile' + autoload :Emoji, 'regexy/text/emoji' end end \ No newline at end of file diff --git a/lib/regexy/text/emoji.rb b/lib/regexy/text/emoji.rb new file mode 100644 index 0000000..9c84531 --- /dev/null +++ b/lib/regexy/text/emoji.rb @@ -0,0 +1,13 @@ +# encoding: UTF-8 + +module Regexy + module Text + class Emoji < Regexy::Regexp + SMILE_REGEX = /([\u{1F600}-\u{1F6FF}])/i + + def initialize(*args) + super(SMILE_REGEX, *args) + end + end + end +end \ No newline at end of file diff --git a/spec/text/emoji_spec.rb b/spec/text/emoji_spec.rb new file mode 100644 index 0000000..2119966 --- /dev/null +++ b/spec/text/emoji_spec.rb @@ -0,0 +1,11 @@ +# encoding: UTF-8 + +describe Regexy::Text::Emoji do + let(:r) { Regexy::Text::Emoji.new } + + it 'accepts valid emoji characters' do + ("\u{1F600}".."\u{1F6FF}").each do |ch| + expect(ch =~ r).to be_truthy + end + end +end \ No newline at end of file From e1c38c452716ae7269d775e982260fe1286de366 Mon Sep 17 00:00:00 2001 From: "v.tikhonov" Date: Tue, 17 Mar 2015 17:54:56 +0300 Subject: [PATCH 3/3] fix rubynius spec --- spec/text/emoji_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/text/emoji_spec.rb b/spec/text/emoji_spec.rb index 2119966..7da6db7 100644 --- a/spec/text/emoji_spec.rb +++ b/spec/text/emoji_spec.rb @@ -4,8 +4,8 @@ let(:r) { Regexy::Text::Emoji.new } it 'accepts valid emoji characters' do - ("\u{1F600}".."\u{1F6FF}").each do |ch| - expect(ch =~ r).to be_truthy + 128512.upto(128767).each do |p| # rubynius doesn't want to iterate through unicode chars + expect([p].pack('U*') =~ r).to be_truthy end end end \ No newline at end of file