regexify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +21 -0
- data/lib/regexify.rb +145 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9840936480b37e468006f50c6105662f33f2f46a1d8f6d4f8b0ec7fb177cbb65
|
4
|
+
data.tar.gz: c996390f7b53f2575d6c9ede816695e46bc533b34b50ed2df3709c15302b8bdf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59366e05a80cdf9df586ae181ab89cda440799ebbb9daf77e9650264a058ce6364e18e9258ab48a189bb9db300c2778209195944f3f391af4cc4c0c6a65bf857
|
7
|
+
data.tar.gz: 939a7c6dd9dfb8a2226aedb6d5dd9515f59405ae24ef94c011458dc2ed4446740fe114081802f8fdba782806dcfcd278e2c125fb8036418a5f0ad25079b80582
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Justin Léger
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/lib/regexify.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# Where the regex magic happens
|
2
|
+
class Regexify
|
3
|
+
|
4
|
+
# Class to raise errors
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
# a small list of popular regex tokens that are available with regexify
|
8
|
+
PATTERNS = {
|
9
|
+
number: '0-9',
|
10
|
+
uppercase: 'A-Z',
|
11
|
+
lowercase: 'a-z',
|
12
|
+
letter: 'a-zA-Z',
|
13
|
+
alphanumeric: 'a-zA-Z0-9',
|
14
|
+
anything: '.',
|
15
|
+
whitespace: '\\s',
|
16
|
+
tab: '\\t',
|
17
|
+
space: ' '
|
18
|
+
}
|
19
|
+
|
20
|
+
# chars that needs to be escaped in a regex
|
21
|
+
ESCAPED_CHARS = %w(* . ? ^ + $ | ( ) [ ] { } \\)
|
22
|
+
|
23
|
+
# default constructor
|
24
|
+
def initialize
|
25
|
+
@str = ""
|
26
|
+
@complete = false
|
27
|
+
end
|
28
|
+
|
29
|
+
# Defines the beginning of the regex and adds `^`
|
30
|
+
# @param args symbols and strings (supporting single and multiple characters)
|
31
|
+
# @param exactly specific number of repetition
|
32
|
+
# @param range range of repetition
|
33
|
+
# @return [Regexify] current regex object
|
34
|
+
def begin_with(*args, exactly: nil, range: nil)
|
35
|
+
raise Regexify::Error.new('#begin_with? called multiple times') unless @str.empty?
|
36
|
+
@str += "^#{parse(args, exactly: exactly, range: range)}"
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
# Defines the ending of the regex and adds `$`
|
41
|
+
# @param args symbols and strings (supporting single and multiple characters)
|
42
|
+
# @param exactly specific number of repetition
|
43
|
+
# @param range range of repetition
|
44
|
+
# @return [Regexify]
|
45
|
+
def end_with(*args, exactly: nil, range: nil)
|
46
|
+
raise Regexify::Error.new('#end_with? called multiple times') if @complete
|
47
|
+
@str += "#{parse(args, exactly: exactly, range: range)}$"
|
48
|
+
@complete = true
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
# Adds a new part to the regex
|
53
|
+
# @param args symbols and strings (supporting single and multiple characters)
|
54
|
+
# @param exactly specific number of repetition
|
55
|
+
# @param range range of repetition
|
56
|
+
# @return [Regexify]
|
57
|
+
def then(*args, exactly: nil, range: nil)
|
58
|
+
@str += parse(args, exactly: exactly, range: range)
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
# Adds a new part to the regex that is negated using `^`
|
63
|
+
# @param args symbols and strings (supporting single and multiple characters)
|
64
|
+
# @param exactly specific number of repetition
|
65
|
+
# @param range range of repetition
|
66
|
+
# @return [Regexify] current regex object
|
67
|
+
def not(*args, exactly: nil, range: nil)
|
68
|
+
@str += parse(args, exactly: exactly, range: range).insert(1, "^")
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
# Converts Regexify object to Regexp
|
73
|
+
def regex
|
74
|
+
Regexp.new(@str)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def parse(args, exactly: nil, range: nil)
|
80
|
+
return_val = ""
|
81
|
+
if args.length == 1
|
82
|
+
return_val = singular_pattern(args.first, extract_regex(args.first))
|
83
|
+
elsif contains_symbols?(args)
|
84
|
+
return_val = "["
|
85
|
+
args.each do |arg|
|
86
|
+
return_val += extract_regex(arg)
|
87
|
+
end
|
88
|
+
return_val += "]"
|
89
|
+
else
|
90
|
+
return_val = "("
|
91
|
+
args.each do |arg|
|
92
|
+
return_val += extract_regex(arg) + "|"
|
93
|
+
end
|
94
|
+
return_val[-1] = ")"
|
95
|
+
end
|
96
|
+
return_val + quantity(exactly, range)
|
97
|
+
end
|
98
|
+
|
99
|
+
def extract_regex(arg)
|
100
|
+
regex_str = ""
|
101
|
+
if arg.is_a? Symbol
|
102
|
+
raise Regexify::Error.new('symbol not defined in patterns') unless PATTERNS.key?(arg)
|
103
|
+
PATTERNS[arg]
|
104
|
+
else
|
105
|
+
escape(arg)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def singular_pattern(arg, pattern)
|
110
|
+
if arg.is_a? Symbol
|
111
|
+
"[#{pattern}]"
|
112
|
+
elsif pattern.length > 1
|
113
|
+
"(#{pattern})"
|
114
|
+
else
|
115
|
+
pattern
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def quantity(exact, range)
|
120
|
+
if range && range.length == 2
|
121
|
+
"{#{range[0]},#{range[1]}}"
|
122
|
+
elsif range
|
123
|
+
"{#{range[0]},}"
|
124
|
+
elsif exact.to_i > 1
|
125
|
+
"{#{exact}}"
|
126
|
+
else
|
127
|
+
""
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def escape(pattern)
|
132
|
+
escaped_pattern = ""
|
133
|
+
pattern.to_s.chars.each do |char|
|
134
|
+
escaped_pattern += ESCAPED_CHARS.include?(char) ? "\\#{char}" : char
|
135
|
+
end
|
136
|
+
escaped_pattern
|
137
|
+
end
|
138
|
+
|
139
|
+
def contains_symbols?(args)
|
140
|
+
args.each do |arg|
|
141
|
+
return true if arg.is_a? Symbol
|
142
|
+
end
|
143
|
+
return false
|
144
|
+
end
|
145
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regexify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Leger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Compact and flexible syntax to generate regular expressions
|
14
|
+
email: hey@justinleger.ca
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- CHANGELOG.md
|
20
|
+
- LICENSE
|
21
|
+
- lib/regexify.rb
|
22
|
+
homepage: https://github.com/jusleg/regexify
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata:
|
26
|
+
source_code_uri: https://github.com/jusleg/regexify
|
27
|
+
changelog_uri: https://github.com/jusleg/regexify/blob/master/CHANGELOG.md
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.7.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Simple regex builder
|
48
|
+
test_files: []
|