0

我想做一些类似“复制链接”的东西,

例如列出的文件:

this is example http://stackoverflow.com and nothing more.
and another way http://www.google.com.

如何只保留 URL?喜欢:

 http://stackoverflow.com
 http://www.google.com

我知道这样的事情:

MatchCollection mc = Regex.Matches(str, "http.+?com", RegexOptions.Singleline);

这是一个很好的代码?如何将结果 ^ 复制到文本文件?

请给我一个例子。

谢谢。

4

1 回答 1

3

我会更严格一点

Protocol, domain name, page and CGI parameters are captured into backreferenes 1 through 4

\b((?#protocol)https?|ftp)://((?#domain)[-A-Z0-9.]+)((?#file)/[-A-Z0-9+&@#/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&@#/%=~_|!:,.;]*)?

甚至

URL: RFC 3986
Validate if a string holds a URL as specified in RFC 3986.  Both absolute and relative URLs are supported. 


(# Scheme
 [a-z][a-z0-9+\-.]*:
 (# Authority & path
  //
  ([a-z0-9\-._~%!$&'()*+,;=]+@)?              # User
  ([a-z0-9\-._~%]+                            # Named host
  |\[[a-f0-9:.]+\]                            # IPv6 host
  |\[v[a-f0-9][a-z0-9\-._~%!$&'()*+,;=:]+\])  # IPvFuture host
  (:[0-9]+)?                                  # Port
  (/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?          # Path
 |# Path without authority
  (/?[a-z0-9\-._~%!$&'()*+,;=:@]+(/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?)?
 )
|# Relative URL (no scheme or authority)
 ([a-z0-9\-._~%!$&'()*+,;=@]+(/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?  # Relative path
 |(/[a-z0-9\-._~%!$&'()*+,;=:@]+)+/?)                            # Absolute path
)
# Query
(\?[a-z0-9\-._~%!$&'()*+,;=:@/?]*)?
# Fragment
(\#[a-z0-9\-._~%!$&'()*+,;=:@/?]*)?
于 2012-06-21T20:53:53.900 回答