regex_generator 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
- data/README.md +18 -1
- data/lib/regex_generator.rb +16 -2
- data/lib/regex_generator/characters_recognizer.rb +6 -0
- data/lib/regex_generator/generator.rb +18 -4
- data/lib/regex_generator/version.rb +1 -1
- metadata +1 -2
- data/lib/regex_generator/' +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9d5ba5472bf6aaea43f8e1d38b9f22c0b77535e203673af54f6bc00f83c99e6
|
4
|
+
data.tar.gz: 98af20a9d68ec8db6a07ec54310690633fff6652c08048db1959413829c5a449
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efd865d82a84fa5e7158b536d219bdbd44c3608a13c165ec700dd75e998f012b6bcf29f7d649f73bfa00e613e4bad2cd1a350004a59dbfbaff301ca9842aa3b6
|
7
|
+
data.tar.gz: d74b1e95d9a7ad2b5bacf70f2cfd616c1821e3454799f2b08484edd7e6f74d5879cf608abc4ebb8a189186101971351b85ecde962bfc8ab7606384c272cf5e3a
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# RegexGenerator
|
1
|
+
# RegexGenerator [](https://badge.fury.io/rb/regex_generator)
|
2
2
|
|
3
3
|
Simple library for generating regular expressions
|
4
4
|
|
@@ -24,6 +24,23 @@ Or install it yourself as:
|
|
24
24
|
RegexGenerator.generate('45', 'some text 45')
|
25
25
|
```
|
26
26
|
|
27
|
+
You can use additional options to generate regex. For example:
|
28
|
+
|
29
|
+
```
|
30
|
+
RegexGenerator.generate('45', 'some text 45', exact_target: true)
|
31
|
+
```
|
32
|
+
|
33
|
+
So far one option `:exact_target` is available. When it `true` regex will
|
34
|
+
generated for exact target value
|
35
|
+
|
27
36
|
## License
|
28
37
|
|
29
38
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
- Fork it
|
43
|
+
- Create your feature branch (git checkout -b my-new-feature)
|
44
|
+
- Commit your changes (git commit -am 'Add some feature')
|
45
|
+
- Push to the branch (git push origin my-new-feature)
|
46
|
+
- Create new Pull Reques
|
data/lib/regex_generator.rb
CHANGED
@@ -5,7 +5,21 @@ require 'regex_generator/characters_recognizer'
|
|
5
5
|
require 'regex_generator/exceptions'
|
6
6
|
|
7
7
|
module RegexGenerator
|
8
|
-
|
9
|
-
|
8
|
+
# rubocop:disable Metrics/LineLength
|
9
|
+
#
|
10
|
+
# Generates regex by text and target text
|
11
|
+
#
|
12
|
+
# @param target [String] what you want to find
|
13
|
+
# @param text [String] source text
|
14
|
+
# @param options [Hash] options to generate regex with
|
15
|
+
# @option options [true, false] :exact_target to generate regex with exact target value
|
16
|
+
# @return [Regexp]
|
17
|
+
#
|
18
|
+
# @example Generate regex
|
19
|
+
# RegexGenerator.generate('45', 'some text 45') #=> /[a-z]+\s[a-z]+(\d+)/
|
20
|
+
#
|
21
|
+
# rubocop:enable Metrics/LineLength
|
22
|
+
def self.generate(target, text, options = {})
|
23
|
+
RegexGenerator::Generator.new(target, text, options).generate
|
10
24
|
end
|
11
25
|
end
|
@@ -2,7 +2,13 @@ module RegexGenerator
|
|
2
2
|
class CharactersRecognizer
|
3
3
|
PATTERNS = [/[A-Z]/, /[a-z]/, /\d/, /\n/, /\s/, /./].freeze
|
4
4
|
|
5
|
+
# Creates array with regex representation for each char from the text
|
6
|
+
#
|
7
|
+
# @param text [String]
|
8
|
+
# @return [Array]
|
5
9
|
def self.recognize(text)
|
10
|
+
return [] unless text
|
11
|
+
|
6
12
|
result = []
|
7
13
|
text.chars.each do |char|
|
8
14
|
PATTERNS.each do |pattern|
|
@@ -1,17 +1,31 @@
|
|
1
1
|
module RegexGenerator
|
2
2
|
class Generator
|
3
|
-
|
3
|
+
# rubocop:disable Metrics/LineLength
|
4
|
+
#
|
5
|
+
# @param target [String] what you want to find
|
6
|
+
# @param text [String] source text
|
7
|
+
# @param options [Hash] options to generate regex with
|
8
|
+
# @option options [true, false] :exact_target to generate regex with exact target value
|
9
|
+
#
|
10
|
+
# rubocop:enable Metrics/LineLength
|
11
|
+
def initialize(target, text, options = {})
|
4
12
|
@text = text
|
5
13
|
@target = target
|
14
|
+
@options = options
|
6
15
|
end
|
7
16
|
|
17
|
+
# @return [Regexp]
|
8
18
|
def generate
|
9
|
-
raise RegexGenerator::TargetNotFoundError unless
|
19
|
+
raise RegexGenerator::TargetNotFoundError unless @text[@target]
|
10
20
|
|
11
21
|
string_patterns_array = slice_to_identicals(recognize(cut_nearest_text))
|
12
|
-
target_patterns_array = slice_to_identicals(recognize(@target))
|
13
22
|
string_regex_str = join_patterns(string_patterns_array)
|
14
|
-
target_regex_str =
|
23
|
+
target_regex_str = if @options[:exact_target]
|
24
|
+
Regexp.escape @target
|
25
|
+
else
|
26
|
+
target_patterns_array = slice_to_identicals(recognize(@target))
|
27
|
+
join_patterns(target_patterns_array)
|
28
|
+
end
|
15
29
|
|
16
30
|
Regexp.new("#{string_regex_str}(#{target_regex_str})")
|
17
31
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: regex_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- o.vykhor
|
@@ -69,7 +69,6 @@ files:
|
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
71
|
- lib/regex_generator.rb
|
72
|
-
- lib/regex_generator/'
|
73
72
|
- lib/regex_generator/characters_recognizer.rb
|
74
73
|
- lib/regex_generator/exceptions.rb
|
75
74
|
- lib/regex_generator/generator.rb
|
data/lib/regex_generator/'
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module RegexGenerator
|
2
|
-
class CharactersRecognizer
|
3
|
-
PATTERNS = [/[A-Z]/, /[a-z]/, /\d/, /\n/, /\s/, /./].freeze
|
4
|
-
|
5
|
-
def self.recognize(text)
|
6
|
-
result = []
|
7
|
-
text.chars.each do |char|
|
8
|
-
PATTERNS.each do |pattern|
|
9
|
-
next unless char[pattern]
|
10
|
-
|
11
|
-
escaped_char = Regexp.escape(char) unless char[/\s/]
|
12
|
-
res_char = escaped_char.eql?(char) ? pattern.source : escaped_char
|
13
|
-
break result << res_char
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
result
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|