Sha256: da85906bbfa954c2359f1300fc77efc8aa58b2a6e39bafb0d7ee7e03206b3920

Contents?: true

Size: 992 Bytes

Versions: 1

Compression:

Stored size: 992 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)
      # TODO: Regex like /\10/ should match the octal representation of their character code,
      # if there is no nth grouped subexpression. For example, `/\10/.examples` should return `["\x08"]`
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regexp-examples-0.5.3 lib/regexp-examples/backreferences.rb