當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby URI.regexp用法及代碼示例

本文簡要介紹ruby語言中 URI.regexp 的用法。

用法

regexp(schemes = nil)

概要

URI::regexp([match_schemes])

參數

match_schemes

Array 個方案。如果給定,則生成的正則表達式與方案為 match_schemes 之一的 URI 匹配。

說明

返回與 URI-like 字符串匹配的 Regexp 對象。此方法返回的 Regexp 對象包括任意數量的捕獲組(括號)。永遠不要依賴它的數量。

用法

require 'uri'

# extract first URI from html_string
html_string.slice(URI.regexp)

# remove ftp URIs
html_string.sub(URI.regexp(['ftp']), '')

# You should not rely on the number of parentheses
html_string.scan(URI.regexp) do |*matches|
  p $&
end

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 URI.regexp。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。