regexes 0.0.0 → 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.
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Regexes
4
- # Wraps a regex and answers a few simple questions about text.
5
- #
6
- # pattern = Regexes::Pattern.new(/\d+/)
7
- # pattern.count("a1 b22 c333") # => 3
8
- # pattern.highlight("a1 b22 c333") # => "a[1] b[22] c[333]"
9
- class Pattern
10
- attr_reader :regex
11
-
12
- def initialize(regex)
13
- @regex = regex.is_a?(Regexp) ? regex : Regexp.new(regex.to_s)
14
- end
15
-
16
- def match?(text)
17
- regex.match?(text)
18
- end
19
-
20
- def matches(text)
21
- text.to_enum(:scan, regex).map { Regexp.last_match[0] }
22
- end
23
-
24
- def count(text)
25
- matches(text).size
26
- end
27
-
28
- def highlight(text, open: "[", close: "]")
29
- text.gsub(regex) { |match| "#{open}#{match}#{close}" }
30
- end
31
-
32
- def to_s
33
- regex.inspect
34
- end
35
- end
36
- end