ruby_regex 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/lib/ruby_regex.rb +41 -0
- data/test/ruby_regex_test.rb +57 -0
- metadata +64 -0
data/README
ADDED
File without changes
|
data/lib/ruby_regex.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module RubyRegex
|
2
|
+
# URL Regex
|
3
|
+
URL = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
|
4
|
+
|
5
|
+
# Domain Regex
|
6
|
+
Domain = /(^$)|(^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?)?$)/ix
|
7
|
+
|
8
|
+
# MasterCard Regex
|
9
|
+
MasterCard = /^5[1-5]\d{14}$/
|
10
|
+
|
11
|
+
# Visa Regex
|
12
|
+
Visa = /^4\d{15}$/
|
13
|
+
|
14
|
+
#
|
15
|
+
# RFC822 Email Address Regex
|
16
|
+
# --------------------------
|
17
|
+
#
|
18
|
+
# Originally written by Cal Henderson
|
19
|
+
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
|
20
|
+
#
|
21
|
+
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
|
22
|
+
#
|
23
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
24
|
+
# http://creativecommons.org/licenses/by-sa/2.5/
|
25
|
+
Email = begin
|
26
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
27
|
+
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
|
28
|
+
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
|
29
|
+
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
|
30
|
+
quoted_pair = '\\x5c[\\x00-\\x7f]'
|
31
|
+
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
|
32
|
+
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
|
33
|
+
domain_ref = atom
|
34
|
+
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
35
|
+
word = "(?:#{atom}|#{quoted_string})"
|
36
|
+
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
37
|
+
local_part = "#{word}(?:\\x2e#{word})*"
|
38
|
+
addr_spec = "#{local_part}\\x40#{domain}"
|
39
|
+
pattern = /\A#{addr_spec}\z/
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'ruby_regex'
|
6
|
+
|
7
|
+
class RubyRegexTest < ActiveSupport::TestCase
|
8
|
+
def test_valid_emails
|
9
|
+
emails = ['test@test.com', 'test@test.co.uk', 'test@test.es', 'test@test.info']
|
10
|
+
emails.each do |email|
|
11
|
+
message = build_message(message, '<?> do not pass the test', email)
|
12
|
+
assert(email =~ RubyRegex::Email, email)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
#TODO: 'test@test' is a valid domain, fix!!!
|
17
|
+
def test_invalid_emails
|
18
|
+
emails = ['test/test.com', 'test', 'test-test.com', 'test.test.com']
|
19
|
+
emails.each do |email|
|
20
|
+
message = build_message(message, '<?> do not pass the test', email)
|
21
|
+
assert(email !~ RubyRegex::Email, message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_valid_domains
|
26
|
+
domains = [ 'test.com', 'www.test.com', 'test.es', 'www.test.es', 'test.co.uk', 'www.test.co.uk', 'test.info', 'www.test.info', 'test.com.es', 'www.test.com.es']
|
27
|
+
domains.each do |domain|
|
28
|
+
message = build_message(message, '<?> do not pass the test', domain)
|
29
|
+
assert(domain =~ RubyRegex::Domain, message)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_invalid_domains
|
34
|
+
domains = [ 'test.', 'www.test.e', 'www.test.', 'test.e', '!test.com', 'test/test.com']
|
35
|
+
domains.each do |domain|
|
36
|
+
message = build_message(message, '<?> do not pass the test', domain)
|
37
|
+
assert(domain !~ RubyRegex::Domain, message)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_valid_url
|
42
|
+
urls = [ 'http://test.com', 'http://www.test.com', 'http://test.es/index', 'http://www.test.es/index.html',
|
43
|
+
'https://test.co.uk', 'http://www.test.co.uk/index.html?id=34&name=username']
|
44
|
+
urls.each do |url|
|
45
|
+
message = build_message('<?> do not pass the test', url)
|
46
|
+
assert(url =~ RubyRegex::URL, message)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_invalid_url
|
51
|
+
urls = [ 'test.com', 'www.test.com', 'http://test.es-index', 'http://www.test.es?index.html']
|
52
|
+
urls.each do |url|
|
53
|
+
message = build_message(message, '<?> do not pass the test', url)
|
54
|
+
assert(url !~ RubyRegex::URL, message)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_regex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Emili Parreno
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-26 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby regular expressions library
|
22
|
+
email: emili@eparreno.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README
|
31
|
+
- lib/ruby_regex.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://www.eparreno.com
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --main
|
39
|
+
- README
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: none
|
63
|
+
test_files:
|
64
|
+
- test/ruby_regex_test.rb
|