Sha256: 1d60cb36299fc62616a3490628b4bf17e33992618b4ef32086fecc5f27af0294

Contents?: true

Size: 793 Bytes

Versions: 6

Compression:

Stored size: 793 Bytes

Contents

module RegexpExamples
  class BackReferenceReplacer
    def substitute_backreferences(full_examples)
      full_examples.map do |full_example|
        begin
          while full_example.match(/__(\w+?)__/)
            full_example.sub!(/__(\w+?)__/, find_backref_for(full_example, $1))
          end
          full_example
        rescue RegexpExamples::BackrefNotFound
          # For instance, one "full example" from /(a|(b)) \2/: "a __2__"
          # should be rejected because the backref (\2) does not exist
          nil
        end
      end.compact
    end

    private
    def find_backref_for(full_example, group_id)
      full_example.all_subgroups.detect do |subgroup|
        subgroup.group_id == group_id
      end || raise(RegexpExamples::BackrefNotFound)
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
regexp-examples-0.5.2 lib/regexp-examples/backreferences.rb
regexp-examples-0.5.1 lib/regexp-examples/backreferences.rb
regexp-examples-0.5.0 lib/regexp-examples/backreferences.rb
regexp-examples-0.4.2 lib/regexp-examples/backreferences.rb
regexp-examples-0.4.1 lib/regexp-examples/backreferences.rb
regexp-examples-0.4.0 lib/regexp-examples/backreferences.rb