regexy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 598e3dce59c516054e30d9b079a7dd93ee8c8ee9
4
- data.tar.gz: 5d861d4f7b6cc4d0d36d6629cf254bce7e9b3696
3
+ metadata.gz: 740a4e8636dc31c08edce639aebe64da44275883
4
+ data.tar.gz: 0385b4f2e8873f0bc9e5e926c5e8c023df03b945
5
5
  SHA512:
6
- metadata.gz: 977c292fcbe5a7a74966006c6580ecc48d4562c89d52e1994be3dd0d227fc18b5b3ab93d813c402c8b1a2f351214971f2314d285731249350e28e86170c6a50a
7
- data.tar.gz: fe3faba108f11c13221a7a9819fa89923ddcdfdf53019ed7c417dc7d809604d0e631749f7b801b86c063e2e571ca728080c28b9b47645d20eba4ccc0b9f87634
6
+ metadata.gz: 2277af27c4c61f1d451941314f157e2a21a629bb73ffd73756f4b7169ac2d5c0b004a054c1c80ca47f05b66de43943f4692b702b394b8e267967261ae767efa0
7
+ data.tar.gz: 89bc96b9096413f9fcc01e513bb2659c1c76724070d0b585d9fedd03922d0f1469b0e9a8ab691ced575313bb9615790576580dabb7eda652edd3228005846f02
data/README.md CHANGED
@@ -12,11 +12,16 @@ Regexy is the ruby gem that contains a lot of common-use regular expressions (su
12
12
  - [Installation](#installation)
13
13
  - [Usage](#usage)
14
14
  * [General usage](#regexyregexp)
15
+ * [Getting the original regexp](#getting-the-original-regexp)
15
16
  * [Combining expressions](#combining-regular-expressions)
17
+ * [Bound and unbound regex](#bound-and-unbound-regular-expressions)
16
18
  * [Email addresses](#regexywebemail)
17
19
  * [Hashtag](#regexywebhashtag)
18
20
  * [IP addresses](#regexywebipv4)
19
21
  * [Url](#regexyweburl)
22
+ * [Hostname](#regexywebhostname)
23
+ * [Smiles](#regexytextsmile)
24
+ * [Emojis](#regexytextemoji)
20
25
  - [Contributing](#contributing)
21
26
 
22
27
  ## Installation
@@ -49,6 +54,13 @@ r4 = Regexy::Regexp.new('foo', Regexp::IGNORECASE) # pass additional configurati
49
54
  'abcfoocde' =~ r1 # => 3
50
55
  r2.match 'abcfoocde' # => #<MatchData "foo">
51
56
  ```
57
+ ### Getting the original regexp
58
+ For methods, that checks if it's arguments `is_a` Regexp instances (for example `String#scan`) you can use `internal_regexp` method.
59
+ ```ruby
60
+ str = 'Email me at first@mail.com or second@mail.com'
61
+ str.scan(Regexy::Web::Email.new.unbound.internal_regexp).map(&:first) # => ["first@mail.com", "second@mail.com"]
62
+ ```
63
+
52
64
  ### Combining regular expressions
53
65
 
54
66
  You can combine your regular expressions with `|` operator using `|` method (or `or`, which is alias for it). Note, that regexp options will be combined too.
@@ -61,11 +73,21 @@ any_ipv4 = Regexy::Web::IPv4.new(:normal) | Regexy::Web::IPv4.new(:with_port) #
61
73
  Also you could simply join two expressions using `+` method, or it's alias `and_then`. Note, that it will __remove__ trailing `\z` from first regex and leading `\A` from second regex.
62
74
  ```ruby
63
75
  Regexy::Regexp.new('foo') + Regexy::Regexp.new(/bar/) # => /foobar/
64
- Regexy::Regexp.new(/foo\z/i) | /bar/ # => /foobar/i
65
- Regexy::Regexp.new(/foo/).or '\Abar' # => /foobar/
66
- Regexy::Regexp.new(/\Afoo\z/).or '\Abar\z' # => /\Afoobar\z/
76
+ Regexy::Regexp.new(/foo\z/i) + /bar/ # => /foobar/i
77
+ Regexy::Regexp.new(/foo/).and_then '\Abar' # => /foobar/
78
+ Regexy::Regexp.new(/\Afoo\z/).and_then '\Abar\z' # => /\Afoobar\z/
67
79
  ```
80
+ ### Bound and unbound regular expressions
81
+ All build-in regular expressions provided in a form of `\A...\z`, which means that they match entire string only. You can remove or add string boundaries using `bound` and `unbound` methods.
82
+ Optional argument `method` available (`:both` by default) - `:left` for manipulating only leading `\A` and `:right` for trailing `\z`.
83
+ ```ruby
84
+ Regexy::Regexp.new('/Afoo/z').unbound(:left) # => /foo\z/
85
+ Regexy::Regexp.new(/foo/i).bound # => /\Afoo\z/i
68
86
 
87
+ # Example - find all ip addresses in the string
88
+ str = '0.0.0.0 and 255.255.255.255 are both valid ip addresses'
89
+ str.scan(Regexy::Web::IPv4.new.unbound.internal_regexp).flatten # => ["0.0.0.0", "255.255.255.255"]
90
+ ```
69
91
  ### Regexy::Web::Email
70
92
 
71
93
  Generates regular expressions for email addresses validation (with unicode support). Available options: `:relaxed` for general sanity check, `:normal` (which is default) with some additional length and ip addresses validations and `:strict` for the paranoids.
@@ -110,5 +132,32 @@ Generates regular expressions for matching Url addresses (with unicode support).
110
132
  r1 = Regexy::Web::Url.new # matches 'http://foo.com', 'www.foo.com' and 'foo.com'
111
133
  ```
112
134
 
135
+ ### Regexy::Web::HostName
136
+
137
+ Generates regular expressions for matching hostname (with unicode support).
138
+
139
+ ```ruby
140
+ r1 = Regexy::Web::HostName.new # matches 'foo.com', 'www.foo.com' and 'киррилический.домен.рф'
141
+ ```
142
+ ### Regexy::Text::Smile
143
+
144
+ Generates regular expressions for matching smiles.
145
+ ```ruby
146
+ r = Regexy::Text::Smile.new # matches ':)', ':=)', 'xD' and so on
147
+ # Find all smiles in text
148
+ str = "Check out http://foo.com :). It's awesome :D"
149
+ str.scan(r.unbound.internal_regexp).map(&:first) # => [":)", ":D"]
150
+ ```
151
+
152
+ ### Regexy::Text::Emoji
153
+
154
+ Generates regular expressions for matching emojis.
155
+ ```ruby
156
+ r = Regexy::Text::Emoji.new # matches '😀','😄' and so on
157
+ # Replace all emojis with 'x_x'
158
+ str = "Check out http://foo.com 😀. It's awesome 😼"
159
+ str.gsub(r.internal_regexp, 'x_x') # => "Check out http://foo.com x_x. It's awesome x_x"
160
+ ```
161
+
113
162
  ## Contributing
114
163
  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).
@@ -1,7 +1,8 @@
1
1
  require 'regexy/version'
2
2
 
3
3
  module Regexy
4
- autoload :Regexp, 'regexy/regexp'
4
+ autoload :Regexp, 'regexy/regexp'
5
5
  autoload :RegexpWithMode, 'regexy/regexp'
6
- autoload :Web, 'regexy/web'
6
+ autoload :Web, 'regexy/web'
7
+ autoload :Text, 'regexy/text'
7
8
  end
@@ -32,6 +32,32 @@ module Regexy
32
32
 
33
33
  alias_method :and_then, :+
34
34
 
35
+ def bound(method = :both)
36
+ new_regexp = source
37
+ method = method.to_sym
38
+ if method == :left || method == :both
39
+ new_regexp.prepend('\A')
40
+ end
41
+ if method == :right || method == :both
42
+ new_regexp.concat('\z')
43
+ end
44
+ new_regexp = additional_bound(method, new_regexp)
45
+ ::Regexy::Regexp.new(new_regexp, options)
46
+ end
47
+
48
+ def unbound(method = :both)
49
+ new_regexp = source
50
+ method = method.to_sym
51
+ if method == :left || method == :both
52
+ new_regexp.sub!(/\A\\A/, '')
53
+ end
54
+ if method == :right || method == :both
55
+ new_regexp.sub!(/\\z\s*\z/, '')
56
+ end
57
+ new_regexp = additional_unbound(method, new_regexp)
58
+ ::Regexy::Regexp.new(new_regexp, options)
59
+ end
60
+
35
61
  protected
36
62
 
37
63
  def normalize_regexp(regexp, *args)
@@ -41,6 +67,14 @@ module Regexy
41
67
  else regexp
42
68
  end
43
69
  end
70
+
71
+ def additional_bound(method, regex) # You can override this methods if your regular expression needs additional bound/unbound logic
72
+ regex
73
+ end
74
+
75
+ def additional_unbound(method, regex)
76
+ regex
77
+ end
44
78
  end
45
79
 
46
80
  class RegexpWithMode < ::Regexy::Regexp
@@ -0,0 +1,6 @@
1
+ module Regexy
2
+ module Text
3
+ autoload :Smile, 'regexy/text/smile'
4
+ autoload :Emoji, 'regexy/text/emoji'
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ module Regexy
4
+ module Text
5
+ class Emoji < Regexy::Regexp
6
+ SMILE_REGEX = /([\u{1F600}-\u{1F6FF}])/i
7
+
8
+ def initialize(*args)
9
+ super(SMILE_REGEX, *args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Regexy
2
+ module Text
3
+ class Smile < Regexy::Regexp
4
+ SMILE_REGEX = /\A((?<![\\:;x])[:8bx;][-=]?[dx\)\(0op\*\#s\\\/](?![\)\(\*\/\\\#]))\z/i
5
+
6
+ def initialize(*args)
7
+ super(SMILE_REGEX, *args)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Regexy
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,9 +1,10 @@
1
1
  module Regexy
2
2
  module Web
3
- autoload :Email, 'regexy/web/email'
4
- autoload :Hashtag, 'regexy/web/hashtag'
5
- autoload :IPv4, 'regexy/web/ip'
6
- autoload :IPv6, 'regexy/web/ip'
7
- autoload :Url, 'regexy/web/url'
3
+ autoload :Email, 'regexy/web/email'
4
+ autoload :Hashtag, 'regexy/web/hashtag'
5
+ autoload :IPv4, 'regexy/web/ip'
6
+ autoload :IPv6, 'regexy/web/ip'
7
+ autoload :Url, 'regexy/web/url'
8
+ autoload :HostName, 'regexy/web/host_name'
8
9
  end
9
10
  end
@@ -3,9 +3,9 @@
3
3
  module Regexy
4
4
  module Web
5
5
  class Email < ::Regexy::RegexpWithMode
6
- EMAIL_RELAXED = /\A\s*[^@\s]+@([^@\s]+\.)+[^@\s]+\s*\z/i.freeze
7
- EMAIL_NORMAL = /\A\s*([^@\s]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i.freeze
8
- EMAIL_STRICT = /\A\s*([-\p{L}\d+._]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i.freeze
6
+ EMAIL_RELAXED = /\A\s*(([^@\s]+)@(([^@\s]+\.)+[^@\s]+))\s*\z/i.freeze
7
+ EMAIL_NORMAL = /\A\s*(([^@\s]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,}))\s*\z/i.freeze
8
+ EMAIL_STRICT = /\A\s*(([-\p{L}\d+._]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,}))\s*\z/i.freeze
9
9
 
10
10
  protected
11
11
 
@@ -3,7 +3,7 @@
3
3
  module Regexy
4
4
  module Web
5
5
  class Hashtag < ::Regexy::Regexp
6
- HASHTAG = /\A#(?=.{2,140}\z)([0-9_\p{L}]*[_\p{L}][0-9_\p{L}]*)\z/u.freeze
6
+ HASHTAG = /\A(#(?=.{2,140}\z)([0-9_\p{L}]*[_\p{L}][0-9_\p{L}]*))\z/ui.freeze
7
7
 
8
8
  def initialize(*args)
9
9
  super(HASHTAG, *args)
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ module Regexy
4
+ module Web
5
+ class HostName < ::Regexy::Regexp
6
+ HOST_NAME = /\A([\p{L}\d_]([\p{L}\d\-_]{0,61}[\p{L}\d])?\.)+[\p{L}]{2,6}\z/i.freeze
7
+
8
+ def initialize(*args)
9
+ super(HOST_NAME, *args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,7 +3,7 @@ module Regexy
3
3
  PORT = /:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\z/i.freeze
4
4
 
5
5
  class IPv4 < ::Regexy::RegexpWithMode
6
- IPV4_NORMAL = /\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z/i.freeze
6
+ IPV4_NORMAL = /\A((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\z/i.freeze
7
7
  IPV4_WITH_PORT = (::Regexy::Regexp.new(IPV4_NORMAL) + PORT.source).freeze
8
8
 
9
9
  protected
@@ -18,13 +18,13 @@ module Regexy
18
18
  end
19
19
 
20
20
  class IPv6 < ::Regexy::RegexpWithMode
21
- IPV6_NORMAL = /\A(?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}(?:[0-9]{1,3}\.){3}
21
+ IPV6_NORMAL = /\A((?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}(?:[0-9]{1,3}\.){3}
22
22
  [0-9]{1,3}(?![:.\w]))(([0-9A-F]{1,4}:){0,5}|:)((:[0-9A-F]{1,4}){1,5}:|:)
23
23
  |::(?:[A-F0-9]{1,4}:){5})(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|
24
24
  [1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|
25
25
  (?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}
26
26
  [A-F0-9]{0,4}(?![:.\w]))(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}
27
- |:)|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})(?![:.\w])\z
27
+ |:)|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})(?![:.\w]))\z
28
28
  /ix.freeze
29
29
 
30
30
  IPV6_WITH_PORT = (::Regexy::Regexp.new(/\A\[/) + IPV6_NORMAL + /\]/ + PORT.source).freeze
@@ -3,13 +3,13 @@
3
3
  module Regexy
4
4
  module Web
5
5
  class Url < ::Regexy::Regexp
6
- URL = /\A([a-z][a-z\d+\-.]*:(\/\/([\p{L}\d\-._~%!$&'()*+,;=]+@)?([\p{L}\d\-._~%]+|
6
+ URL = /\A(([a-z][a-z\d+\-.]*:(\/\/([\p{L}\d\-._~%!$&'()*+,;=]+@)?([\p{L}\d\-._~%]+|
7
7
  \[[\p{L}\d:.]+\]|\[v[a-f0-9][\p{L}\d\-._~%!$&'()*+,;=:]+\])(:[0-9]+)?
8
8
  (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?|(\/?[\p{L}\d\-._~%!$&'()*+,;=:@]+
9
9
  (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?)?)|([\p{L}\d\-._~%!$&'()*+,;=@]+
10
10
  (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?|(\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)
11
11
  +\/?))
12
- (\?[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?(\#[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?\z
12
+ (\?[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?(\#[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?)\z
13
13
  /ix.freeze
14
14
 
15
15
  def initialize(*args)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regexy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Tikhonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-15 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -84,10 +84,14 @@ files:
84
84
  - Rakefile
85
85
  - lib/regexy.rb
86
86
  - lib/regexy/regexp.rb
87
+ - lib/regexy/text.rb
88
+ - lib/regexy/text/emoji.rb
89
+ - lib/regexy/text/smile.rb
87
90
  - lib/regexy/version.rb
88
91
  - lib/regexy/web.rb
89
92
  - lib/regexy/web/email.rb
90
93
  - lib/regexy/web/hashtag.rb
94
+ - lib/regexy/web/host_name.rb
91
95
  - lib/regexy/web/ip.rb
92
96
  - lib/regexy/web/url.rb
93
97
  - regexy.gemspec