Skip to content

Commit

Permalink
Smiles matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-tikhonov committed Mar 17, 2015
1 parent 55517e7 commit 92c919b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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).
5 changes: 3 additions & 2 deletions 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
5 changes: 5 additions & 0 deletions lib/regexy/text.rb
@@ -0,0 +1,5 @@
module Regexy
module Text
autoload :Smile, 'regexy/text/smile'
end
end
11 changes: 11 additions & 0 deletions lib/regexy/text/smile.rb
@@ -0,0 +1,11 @@
module Regexy
module Text
class Smile < Regexy::Regexp
SMILE_REGEX = /\A((?<![\\:;x])[:8bx;][-=]?[dx\)\(0op\*\#s\\\/](?![\)\(\*\/\\\#]))\z/i

def initialize(*args)
super(SMILE_REGEX, *args)
end
end
end
end
35 changes: 35 additions & 0 deletions spec/text/smile_spec.rb
@@ -0,0 +1,35 @@
describe Regexy::Text::Smile do
VALID_SMILES = [
':)', ':=)', ':-)', ':(',
':=(', ':-(', ':D', ':=D',
':-D', ':d', ':=d', ':-d',
'8=)', '8-)', 'B=)', 'B-)',
';)', ';-)', ';=)', ':o',
':=o', ':-o', ':O', ':=O',
':-O', ';(', ';-(', ';=(',
':*', ':=*', ':-*', ':P',
':=P', ':-P', ':p', ':=p',
':-p', ':S', ':-S', ':=S',
':s', ':-s', ':=s', ':x',
':-X', ':#', ':-#', ':=x',
':=X', ':=#', 'xd', ':\\', ':D', ':/'
]

INVALID_SMILES = [
'://', '::)'
]

let(:r) { Regexy::Text::Smile.new }

it 'accept valid smiles' do
VALID_SMILES.each do |smile|
expect(smile =~ r).to be_truthy
end
end

it 'does not accept invalid smiles' do
INVALID_SMILES.each do |smile|
expect(smile =~ r).to be_nil
end
end
end

0 comments on commit 92c919b

Please sign in to comment.