2

我有一个这样的正则表达式;

/(?:http[s]?:\/\/)?(?:www\.)?(.*)/

我只想捕获任何不考虑协议的网站的域部分。所以,这个正则表达式有效,但它也捕获了一些空格。这里的空白从何而来?如果我理解这一点,它可能对我很有帮助。

如果我输入以下字符串进行匹配,

输入:

http://google.com
https://www.simplecms.com
www.facebook.com
gmail.com

输出:

Match 1
1.  google.com
Match 2
1.   
Match 3
1.  simplecms.com
Match 4
1.   
Match 5
1.  facebook.com
Match 6
1.   
Match 7
1.  gmail.com
Match 8
1.   
Match 9
1.    

如果您查看输出,它会完成这项工作,但它也会捕获一些空字符串。我相信有更好的方法来捕捉这个东西。

4

5 回答 5

2

尝试在正则表达式的开头和结尾添加行首/行尾。 ^开头$结尾,匹配就行了。我认为正在发生的事情是您的正则表达式看到了回报并匹配了这些回报。

尝试:(^(?:http[s]?:\/\/)?(?:www\.)?(.*)$ 注意:您可能需要根据语言转义)。

使用您提供的示例,我设置了一个快速测试以在此处显示匹配项

于 2013-09-04T15:48:20.613 回答
0

我可以使用这种非正则表达式方法:-

require 'uri'

str = <<-eot
http://google.com
https://www.simplecms.com
www.facebook.com
gmail.com
eot

str.split.map{|u| uri=URI(u); uri.scheme ? uri.host : u}
# => ["google.com", "www.simplecms.com", "www.facebook.com", "gmail.com"]
于 2013-09-04T15:47:44.220 回答
0

不要尝试重新创建用于解析 URL 的模式。

这是使用 Ruby 的URI类提供给您的模式的快速而肮脏的列表。您不需要直接访问这些,因为有些方法提供了处理模式无法处理的情况所需的逻辑。

实际定义在 URI 的common.rb 中

require 'uri'

URI::REGEXP::PATTERN.constants.each do |s|
  puts ':%s => %%r/%s/x' % [s, URI::REGEXP::PATTERN.const_get(s)]
end

哪个输出:

:ALPHA => %r/a-zA-Z/x
:ALNUM => %r/a-zA-Z\d/x
:HEX => %r/a-fA-F\d/x
:ESCAPED => %r/%[a-fA-F\d]{2}/x
:UNRESERVED => %r/\-_.!~*'()a-zA-Z\d/x
:RESERVED => %r/;/?:@&=+$,\[\]/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'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*)*)?)(?:\?(?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*)?)?(?:#(?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*)?/x
:X_ABS_URI => %r/
        ([a-zA-Z][\-+.a-zA-Z\d]*):                           (?# 1: scheme)
        (?:
           ((?:[\-_.!~*'()a-zA-Z\d;?:@&=+$,]|%[a-fA-F\d]{2})(?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*)                    (?# 2: opaque)
        |
           (?:(?:
             //(?:
                 (?:(?:((?:[\-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)?        (?# 3: userinfo)
                   (?:((?:(?:[a-zA-Z0-9\-.]|%\h\h)+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))(?::(\d*))?))? (?# 4: host, 5: port)
               |
                 ((?:[\-_.!~*'()a-zA-Z\d$,;:@&=+]|%[a-fA-F\d]{2})+)                 (?# 6: registry)
               )
             |
             (?!//))                           (?# XXX: '//' is the mark for hostport)
             (/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*(?:/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*)*)?                    (?# 7: path)
           )(?:\?((?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?                 (?# 8: query)
        )
        (?:\#((?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?                  (?# 9: fragment)
      /x
:X_REL_URI => %r/
        (?:
          (?:
            //
            (?:
              (?:((?:[\-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)?       (?# 1: userinfo)
                ((?:(?:[a-zA-Z0-9\-.]|%\h\h)+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))?(?::(\d*))?  (?# 2: host, 3: port)
            |
              ((?:[\-_.!~*'()a-zA-Z\d$,;:@&=+]|%[a-fA-F\d]{2})+)             (?# 4: registry)
            )
          )
        |
          ((?:[\-_.!~*'()a-zA-Z\d;@&=+$,]|%[a-fA-F\d]{2})+)              (?# 5: rel_segment)
        )?
        (/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*(?:/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*)*)?                  (?# 6: abs_path)
        (?:\?((?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?              (?# 7: query)
        (?:\#((?:[\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?           (?# 8: fragment)
      /x

URI 期望字符串以某种方案开头。当您知道主机是一个网站时,您可以通过在前面加上“http://”来欺骗它,或者直接使用主机。如果传入的字符串不仅仅是主机,请在前面加上“http://”并让 URI 剥离字符串的各个部分:

require 'uri'

hosts = %w[
  http://google.com
  https://www.simplecms.com
  www.facebook.com
  gmail.com
  http://foo.com/posts?id=30&limit=5#time=1305298413
  https://foo.com/posts?id=30&limit=5#time=1305298413
  foo.com/posts?id=30&limit=5#time=1305298413
  ].map { |url|

    scheme, userinfo, host, port, registry, path, opaque, query, fragment = case !!url[%r(\Ahttps?://)i]
    when true
      URI.split(url)
    when false
      URI.split("http://#{ url }")
    end

    host
  }
hosts # => ["google.com", "www.simplecms.com", "www.facebook.com", "gmail.com", "foo.com", "foo.com", "foo.com"]

您也可以直接访问主机组件:

url = "http://foo.com/posts?id=30&limit=5#time=1305298413"
uri = URI(url)
uri.host # => "foo.com"

scheme, userinfo, host, port, registry, path, opaque, query, fragment = URI.split("http://#{ url }") 
scheme # => "http"
userinfo # => nil
host # => "http"
port # => ""
registry # => nil
path # => "//foo.com/posts"
opaque # => nil
query # => "id=30&limit=5"
fragment # => "time=1305298413"

上面关于希望查看方案的 URI 的警告仍然适用。

URI 是“FTW”,因为它可以拆分字符串,并允许您访问每个组件。它还允许您构建或修改 URI,并以符合 RFC 的方式进行。

如果 URI 不够复杂,还有Addressable,它甚至更 FTW。

于 2013-09-04T16:39:31.570 回答
0

一种适用于您的用例的更简单的方法:

> sites
 => ["http://google.com", "https://www.simplecms.com", "www.facebook.com", "gmail.com"] 
> sites.map{|s| s.split('/').last}
 => ["google.com", "www.simplecms.com", "www.facebook.com", "gmail.com"] 
于 2013-09-04T16:46:07.600 回答
0

而不是使用正则表达式,只需使用内置的URI 解析器

require 'uri'

def get_domain(url)
  url = 'http://' + url unless url =~ /^https?:\/\//i
  begin
    URI.parse(url).host
  rescue URI::InvalidURIError
    nil
  end
end

get_domain('http://google.com')         # => google.com
get_domain('https://www.simplecms.com') # => www.simplecms.com
get_domain('www.facebook.com')          # => www.facebook.com
get_domain('gmail.com')                 # => gmail.com
于 2013-09-04T20:51:16.040 回答