class UrlExtract::Match
A URL that the extractor found in a text:
UrlExtract.scan("See https://example.com/x").first # => #<UrlExtract::Match url="https://example.com/x" range=4...25>
Attributes
range
[R]
Range[Integer]
Returns the place of the URL in the text, in characters:
match = UrlExtract.scan("See https://example.com/x").first match.range # => 4...25 match.text[match.range] # => "https://example.com/x"
To replace the URLs of a text, go from the last match to the first: a replacement changes the length of the text, and moves each URL after it:
text = "See https://example.com/x and https://example.org/y" UrlExtract.scan(text).reverse_each { |match| text[match.range] = "<FILTERED>" } text # => "See <FILTERED> and <FILTERED>"
text
[R]
String
Returns a frozen copy of the text, as it was when the extractor read it. The matches of one text share it:
text = +"See https://example.com/x" match = UrlExtract.scan(text).first text << " More." match.text # => "See https://example.com/x" match.text.frozen? # => true
url
[R]
String
Returns the URL as the text writes it. In HTML it can hold a character reference, such as &:
UrlExtract.scan("See https://example.com/x").first.url # => "https://example.com/x" UrlExtract.scan(%(<a href="https://example.com/x?a=1&b=2">)).first.url # => "https://example.com/x?a=1&b=2"
Public Class Methods
(String url, Integer index, String text) → void
Source
# File lib/url_extract/match.rb, line 48 def initialize(url, index, text) @url = url @range = index...(index + url.length) @text = text end
The extractor makes each match, from the index of the URL in the text.
Public Instance Methods
() → String
Source
# File lib/url_extract/match.rb, line 75 def inspect "#<#{self.class} url=#{url.inspect} range=#{range.inspect}>" end
Returns a string that shows the URL and the range, and not the text, which can be large:
UrlExtract.scan("See https://example.com/x").first # => #<UrlExtract::Match url="https://example.com/x" range=4...25>
() → String
Source
# File lib/url_extract/match.rb, line 63 def to_s url end
Returns the URL:
match = UrlExtract.scan("See https://example.com/x").first "Link: #{match}" # => "Link: https://example.com/x"