regexp-examples 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmEzZDkwZTIyMTM0MzM1MTQ3YThmY2NhNGJmNGQzZDZmYTdlOTgzZQ==
4
+ OWJkMDYzMDJhNjQ2ZWE1YmZjZjM4NWViMThmYWVmMjA0ODBkZmZhNg==
5
5
  data.tar.gz: !binary |-
6
- ZTdlYjhjOTg2MTc1MWU5M2NjYTk1ODU2OGNiNWMxYTIxYzlhMjdlMg==
6
+ Yjk3YTM5ZjNiMTdkODEwNmVjZGM2YTMzMTMzODA3OTE5YWFiMjE5Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTUyY2Q2MzUwNjczMmYwOWQ1YTJkZTk5N2I2YWIzM2Q4NzUxODM0NDg4ZWQ3
10
- ZDJiYTVkM2RhYzE1NTAzODdjMzY4ZDI4NTM3NjM0ZjJiZWQ1N2U4ZmVmMzIz
11
- M2YyMDQ1MTAyZDg5NjAwOTY5Y2FlMDIyNzkwZjdhMDJiOTZhNDU=
9
+ MDc5YTA3MTdkMWQ0MmQ4OTk5Mzg4MjRlMWNhOTUxMTA0NWZjZDY3MmY1ZDc5
10
+ YjE2MWM3YTgxODgwZmFkYmEwMDY4MzdjZDg3OTQ1NWE5ZDFjNWE4NTIzYjhh
11
+ OWY1OTk4ZWU3ODE2YWIxMTg2Mzg0ZmQxYjNkNzgzNjNmNzU1ZDU=
12
12
  data.tar.gz: !binary |-
13
- ZmQ1NWY5NjRiYzViNjdlNjNhODQ1ODkyZTZmMzk5MTIzZDgxZDFjNDIxODEz
14
- ODY5ZDcxYzEwODAxYjhmNDE5MGM1ZDk5YzkxYjk5ZTUxYzRkYjY5ZTM2MWNk
15
- NTNkZmRjYmMzN2ZkNjg4MDQ1MzdlNWNhZjYzOTJlODVkZjQwMTE=
13
+ MTJiMjA3NjY5ZDk0ODJjNDVmYjk5ZjczNjU5MDk4OGQ1N2NjZWNhODFiNDVj
14
+ Y2IxZGFmYzZhMzgzYTZhOTdiNDYwYWM4YzA0NDg3NmM1YzIwOWE5ZTc2ODNl
15
+ ZGMzMTdlZjhiYTY2NDI4NGIwNTVlOWViYjhjYzNmMmNlMGUxNGQ=
data/README.md CHANGED
@@ -29,9 +29,12 @@ or a huge number of possible matches, such as `/.\w/`, then only a subset of the
29
29
  * Boolean "Or" groups, e.g. `/a|b|c/`
30
30
  * Character sets (inluding ranges and negation!), e.g. `/[abc]/`, `/[A-Z0-9]/`, `/[^a-z]/`
31
31
  * Escaped characters, e.g. `/\n/`, `/\w/`, `/\D/` (and so on...)
32
- * Capture groups, including named groups and backreferences(!!), e.g. `/(this|that) \1/` `/(?<name>foo) \k<name>/`
33
32
  * Non-capture groups, e.g. `/(?:foo)/`
34
- * '''Arbitrarily complex combinations of all the above!'''
33
+ * Capture groups, e.g. `/(group)/`
34
+ * Including named groups, e.g. `/?(<name>group)/`
35
+ * ...And backreferences(!!!), e.g. `/(this|that) \1/` `/(?<name>foo) \k<name>/`
36
+ * Groups work fine, even if nested! e.g. `/(even(this(works?))) \1 \2 \3/`
37
+ * **Arbitrarily complex combinations of all the above!**
35
38
 
36
39
  ## Not-Yet-Supported syntax
37
40
 
@@ -7,10 +7,15 @@ module RegexpExamples
7
7
  super(values)
8
8
  end
9
9
 
10
+ def all_subgroups
11
+ [self, subgroups].flatten
12
+ end
13
+
10
14
  # Overridden in order to preserve the @group_id and @subgroups
11
15
  def *(int)
12
16
  self.class.new(group_id, subgroups, super)
13
17
  end
18
+ # Overridden in order to preserve the @group_id and @subgroups
14
19
  def gsub(regex)
15
20
  self.class.new(group_id, subgroups, super)
16
21
  end
@@ -36,16 +41,10 @@ module RegexpExamples
36
41
  def find_backref_for(full_example, group_id)
37
42
  full_example.each do |partial_example|
38
43
  next unless partial_example.respond_to?(:group_id)
39
- case
40
- when partial_example.group_id == group_id
41
- return partial_example
42
- when sub_partial_example = find_backref_for(partial_example.subgroups, group_id)
43
- # TODO: This line does NOT work for all nested backreference groups
44
- # Need to revisit this logic and find a better solution, if possible
45
- return sub_partial_example.result.detect{|sub_partial_result| partial_example.include? sub_partial_result}
46
- end
44
+ partial_example.all_subgroups.each do |subgroup|
45
+ return subgroup if subgroup.group_id == group_id
46
+ end
47
47
  end
48
- nil
49
48
  end
50
49
 
51
50
  end
@@ -78,8 +78,8 @@ module RegexpExamples
78
78
  # itself
79
79
  def result
80
80
  strings = @groups.map {|repeater| repeater.result}
81
- subgroups = @groups.select{ |repeater| repeater.group.respond_to? :group_id}.map{|repeater| repeater.group}
82
81
  RegexpExamples::permutations_of_strings(strings).map do |result|
82
+ subgroups = result.respond_to?(:group_id) ? result.all_subgroups : []
83
83
  group_id ? CaptureGroupResult.new(group_id, subgroups, result) : result
84
84
  end
85
85
  end
@@ -11,7 +11,25 @@ module RegexpExamples
11
11
  first = arrays_of_strings.shift
12
12
  return first if arrays_of_strings.empty?
13
13
  first.product( permutations_of_strings(arrays_of_strings, options) ).map do |result|
14
- options[:no_join] ? result.flatten : result.flatten.join
14
+ if options[:no_join]
15
+ result.flatten
16
+ else
17
+ join_preserving_capture_groups(result)
18
+ end
19
+ end
20
+ end
21
+
22
+ def self.join_preserving_capture_groups(result)
23
+ result.flatten!
24
+ subgroups = result
25
+ .select { |partial| partial.respond_to? :group_id }
26
+ .map(&:all_subgroups)
27
+ .flatten
28
+
29
+ if subgroups.empty?
30
+ result.join
31
+ else
32
+ CaptureGroupResult.new(nil, subgroups, result.join)
15
33
  end
16
34
  end
17
35
 
@@ -1,3 +1,3 @@
1
1
  module RegexpExamples
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -86,7 +86,9 @@ describe Regexp, "#examples" do
86
86
  /(repeat) \1/,
87
87
  /(ref1) (ref2) \1 \2/,
88
88
  /((ref2)ref1) \1 \2/,
89
- /((ref1and2)) \1 \2/
89
+ /((ref1and2)) \1 \2/,
90
+ /(one)(two)(three)(four)(five)(six)(seven)(eight)(nine)(ten) \10\9\8\7\6\5\4\3\2\1/,
91
+ /(a?(b?(c?(d?(e?)))))/
90
92
  )
91
93
  end
92
94
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regexp-examples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lord