Ruby on rails 如何组合Ruby regexp条件

Ruby on rails 如何组合Ruby regexp条件,ruby-on-rails,ruby,regex,Ruby On Rails,Ruby,Regex,我需要检查字符串是否是有效的图像url。 我想检查字符串的开头和结尾,如下所示: 必须以http开头: 必须以.jpg |.png |.gif |.jpeg结尾 到目前为止,我已经: (https?:) 我似乎无法指示字符串\A的开头、组合模式和测试字符串的结尾 测试字符串: "http://image.com/a.jpg" "https://image.com/a.jpg" "ssh://image.com/a.jpg" "http://image.com/a.jpeg" "https:

我需要检查字符串是否是有效的图像url。 我想检查字符串的开头和结尾,如下所示:

  • 必须以http开头:
  • 必须以.jpg |.png |.gif |.jpeg结尾
到目前为止,我已经:

(https?:)
我似乎无法指示字符串
\A
的开头、组合模式和测试字符串的结尾

测试字符串:

"http://image.com/a.jpg"
"https://image.com/a.jpg"
"ssh://image.com/a.jpg"
"http://image.com/a.jpeg"
"https://image.com/a.png"
"ssh://image.com/a.jpeg"
请看


使用Ruby 2.5

和您自己的演示,您可以使用

^https?:\/\/.*(?:\.jpg|\.png|\.gif|\.jpeg)$
请参阅。

甚至可以简化为:

^https?:\/\/.*\.(?:jpe?g|png|gif)$
看。
这主要是在两侧使用锚(
^
$
)来指示字符串的开始/结束。此外,请记住,如果您想使用
,则需要转义圆点(
\.
)。
在评论部分有一些模棱两可的地方,所以让我澄清一下:

^  - is meant for the start of a string 
     (or a line in multiline mode, but in Ruby strings are always in multiline mode)
$  - is meant for the end of a string / line
\A - is the very start of a string (irrespective of multilines) 
\z - is the very end of a string (irrespective of multilines) 
你可以用

reg = %r{\Ahttps?://.*\.(?:png|gif|jpe?g)\z}
重点是:

  • 在在线正则表达式测试器上测试时,您正在测试单个多行字符串,但在现实生活中,您将验证作为单独字符串的行。因此,在这些测试仪中,使用
    ^
    $
    ,在真实代码中,使用
    \A
    \z
  • 要匹配字符串而不是行,您需要
    \a
    \z
    锚定
  • 使用
    %r{pat}
    语法如果您的模式中有许多
    /
    ,那么它会更干净
  • :

    输出:

    http://image.com/a.jpg: true
    https://image.com/a.jpg: true
    ssh://image.com/a.jpg: false
    http://image.com/a.jpeg: true
    https://image.com/a.png: true
    ssh://image.com/a.jpeg: false
    

    这里的答案很好,但是如果您想避免使用复杂的正则表达式,并向读者更清楚地传达您的意图,您可以让
    URI
    File
    为您完成繁重的工作

    (既然您使用的是2.5,那么让我们使用
    #match?
    而不是其他正则表达式匹配方法。)


    这显然与
    “foo\nhttp…”jpg\nbar“
    匹配,它既不以
    “http”
    开头,也不以
    “jpg”
    结尾。除非您完全了解情况,否则请始终使用
    \A
    \z
    锚定,而不是
    ^
    $
    。我认为您不需要第二次分组:
    ^https?:\/\/.*。(?:jpe?g | png | gif)$
    @revo我擅长阅读作品的第一句。它指出:“我需要检查字符串是否为有效的图像url。”@mudasobwa和我希望您能够很好地阅读OP提供的演示,其中每行都有URL?@revo:谢谢,这正是使用
    ^
    $
    而不是
    \a
    \Z
    的意义所在。因此,答案部分的人们正在争论您的“测试字符串”是否(1)许多字符串,每行一个,没有空格或(2)一个正则表达式必须匹配每行的单个字符串。您能编辑问题以澄清这一点吗?谢谢您的解释。非常感谢
    http://image.com/a.jpg: true
    https://image.com/a.jpg: true
    ssh://image.com/a.jpg: false
    http://image.com/a.jpeg: true
    https://image.com/a.png: true
    ssh://image.com/a.jpeg: false
    
    def valid_url?(url)
      # Let URI parse the URL.
      uri = URI.parse(url)
      # Is the scheme http or https, and does the extension match expected formats?
      uri.scheme.match?(/https?/i) && File.extname(uri.path).match?(/(png|jpe?g|gif)/i)
    rescue URI::InvalidURIError
      # If it's an invalid URL, URI will throw this error.
      # We'll return `false`, because a URL that can't be parsed by URI isn't valid.
      false
    end
    
    urls.map { |url| [url, valid_url?(url)] }
    
    #=> Results in:
    'http://image.com/a.jpg', true
    'https://image.com/a.jpg', true
    'ssh://image.com/a.jpg', false
    'http://image.com/a.jpeg', true
    'https://image.com/a.png', true
    'ssh://image.com/a.jpeg', false
    'https://image.com/a.tif', false
    'http://t.co.uk/proposal.docx', false
    'not a url', false