matthewrudy-regexpert 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +18 -0
- data/Rakefile +23 -0
- data/lib/regexpert.rb +76 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Regexpert
|
2
|
+
=========
|
3
|
+
|
4
|
+
Regexpert is an attempt to remove the need to worry about which regular expression to use for validating an email, a postcode, or a social security number.
|
5
|
+
Its inspired by the django.contrib.localflavor libraries and Datamapper dm-more's dm-validations module.
|
6
|
+
|
7
|
+
Example
|
8
|
+
=======
|
9
|
+
|
10
|
+
Instead of googling "email regexp", and then coming up with numerous arguments about the right regexp to use,
|
11
|
+
just use Regexpert
|
12
|
+
|
13
|
+
validates_format_of :email, :with => Regexpert::Format::EmailAddress
|
14
|
+
|
15
|
+
BOOM!!!
|
16
|
+
no work, no worries.
|
17
|
+
|
18
|
+
Copyright (c) 2008 [Matthew Rudy Jacobs], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the regexpert plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the regexpert plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Regexpert'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/lib/regexpert.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module Regexpert
|
2
|
+
module Format
|
3
|
+
# This is taken from dm-more - http://github.com/sam/dm-more/tree/master/dm-validations/lib/dm-validations/formats/email.rb
|
4
|
+
# RFC2822 (No attribution reference available)
|
5
|
+
#
|
6
|
+
# doctest: email_address
|
7
|
+
# >> "MatthewRudyJacobs@gmail.com" =~ Regexpert::Format::EmailAddress
|
8
|
+
# => 0
|
9
|
+
#
|
10
|
+
# >> "dev@" =~ Regexpert::Format::EmailAddress
|
11
|
+
# => nil
|
12
|
+
#
|
13
|
+
EmailAddress = begin
|
14
|
+
alpha = "a-zA-Z"
|
15
|
+
digit = "0-9"
|
16
|
+
atext = "[#{alpha}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
|
17
|
+
dot_atom_text = "#{atext}+([.]#{atext}*)*"
|
18
|
+
dot_atom = "#{dot_atom_text}"
|
19
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
20
|
+
text = "[\\x01-\\x09\\x11\\x12\\x14-\\x7f]"
|
21
|
+
quoted_pair = "(\\x5c#{text})"
|
22
|
+
qcontent = "(?:#{qtext}|#{quoted_pair})"
|
23
|
+
quoted_string = "[\"]#{qcontent}+[\"]"
|
24
|
+
atom = "#{atext}+"
|
25
|
+
word = "(?:#{atom}|#{quoted_string})"
|
26
|
+
obs_local_part = "#{word}([.]#{word})*"
|
27
|
+
local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
|
28
|
+
no_ws_ctl = "\\x01-\\x08\\x11\\x12\\x14-\\x1f\\x7f"
|
29
|
+
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
30
|
+
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
31
|
+
domain_literal = "\\[#{dcontent}+\\]"
|
32
|
+
obs_domain = "#{atom}([.]#{atom})*"
|
33
|
+
domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
|
34
|
+
addr_spec = "#{local_part}\@#{domain}"
|
35
|
+
pattern = /^#{addr_spec}$/
|
36
|
+
end
|
37
|
+
|
38
|
+
# This is taken from dm-more http://github.com/sam/dm-more/tree/master/dm-validations/lib/dm-validations/formats/url.rb
|
39
|
+
# Regex from http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/
|
40
|
+
#
|
41
|
+
# doctest: url # examples from Rails auto_link tests
|
42
|
+
# >> "http://www.rubyonrails.com/contact;new" =~ Regexpert::Format::Url
|
43
|
+
# => 0
|
44
|
+
# >> "http://maps.google.co.uk/maps?f=q&q=the+london+eye&ie=UTF8&ll=51.503373,-0.11939&spn=0.007052,0.012767&z=16&iwloc=A" =~ Regexpert::Format::Url
|
45
|
+
# => 0
|
46
|
+
# >> "http://en.wikipedia.org/wiki/Sprite_(computer_graphics)" =~ Regexpert::Format::Url
|
47
|
+
# => 0
|
48
|
+
# TODO: think of a good example of a bad url
|
49
|
+
Url = begin
|
50
|
+
/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
|
51
|
+
end
|
52
|
+
|
53
|
+
# This is taken from Django.Contrib.Localflavor.uk
|
54
|
+
# The regular expression used is sourced from the schema for British Standard
|
55
|
+
# BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
|
56
|
+
#
|
57
|
+
# doctest: ukpostcode
|
58
|
+
# >> "GIR 0AA" =~ Regexpert::Format::UKPostcode # GIR 0AA is a special GIRO postcode
|
59
|
+
# => 0
|
60
|
+
# >> "AL40XB" =~ Regexpert::Format::UKPostcode
|
61
|
+
# => 0
|
62
|
+
# >> "CB4 1TL" =~ Regexpert::Format::UKPostcode
|
63
|
+
# => 0
|
64
|
+
#
|
65
|
+
# >> "AL44 NOP" =~ Regexpert::Format::UKPostcode
|
66
|
+
# => nil
|
67
|
+
# >> "CB4-1TL" =~ Regexpert::Format::UKPostcode
|
68
|
+
# => nil
|
69
|
+
#
|
70
|
+
UKPostcode = begin
|
71
|
+
outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])'
|
72
|
+
incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}'
|
73
|
+
postcode_regex = Regexp.new("^(GIR *0AA|#{outcode_pattern} *#{incode_pattern})$", Regexp::IGNORECASE)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matthewrudy-regexpert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Rudy Jacobs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-07 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Regular expressions bundled into a single place, so you dont have to think anymore about what email regexp to use.
|
17
|
+
email: MatthewRudyJacobs@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- Rakefile
|
27
|
+
- lib/regexpert.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/matthewrudy/regexpert
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --main
|
33
|
+
- README
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: A bunch of useful Regular expressions
|
55
|
+
test_files: []
|
56
|
+
|