【问题标题】:Java regex for URL用于 URL 的 Java 正则表达式
【发布时间】:2015-12-13 08:13:00
【问题描述】:

我需要一个正则表达式,它将匹配下一个 url:

(http|https)://(www)<my domain here>
(http|https)://(www)<my domain here>/page1
(http|https)://(www)<my domain here>/page1/.../
(http|https)://(www)<my domain here>/page1/...?a=b
(http|https)://(www)<my domain here>/page1/...?a=b&c=d...

我有一个正则表达式,但我不知道如何编辑它

^(http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?$

【问题讨论】:

    标签: java regex url


    【解决方案1】:

    您可以使用以下内容:

    ^(http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?(\/[a-z0-9])*(\/?|(\?[a-z0-9]=[a-z0-9](&[a-z0-9]=[a-z0-9]*)?))$
    

    对于特定的域名:

    ^(http:\/\/|https:\/\/)?(www.)?example\.com(\/?|(\?[a-z0-9]=[a-z0-9](&[a-z0-9]=[a-z0-9]*)?))$
    

    编辑:验证域并获取 URL:

    (http:\/\/|https:\/\/)?(www.)?example\.com\S*
    

    【讨论】:

    • 你不明白我的意思,我只想获取具有特定域名的网址,例如:example.org... 我应该在你的正则表达式中将“example.org”放在哪里?
    • @Sevak.Avet 更新了答案。我以为你想要验证 /page1/...?a=b.. 等。:P
    • 我的意思是,我想获取特定域的完整 URL :) 我只想验证域,但获取完整 URL
    【解决方案2】:

    试试这个:

    ^https?://[^/@]*\.domain\.com(/.*)?$
    

    【讨论】:

      【解决方案3】:

      试试这个:

      ^(http|https):\/\/(www).([a-z\.]*)?(\/[a-z1-9\/]*)*\??([\&a-z1-9=]*)?
      

      我在网站Regular expressions 101得到的解释。

      /^(http|https):\/\/(www).([a-z\.]*)?(\/[a-z1-9\/]*)*\??([\&a-z1-9=]*)?/
          ^ assert position at start of the string
          1st Capturing group (http|https)
              1st Alternative: http
                  http matches the characters http literally (case sensitive)
              2nd Alternative: https
                  https matches the characters https literally (case sensitive)
          : matches the character : literally
          \/ matches the character / literally
          \/ matches the character / literally
          2nd Capturing group (www)
              www matches the characters www literally (case sensitive)
          . matches any character (except newline)
          3rd Capturing group ([a-z\.]*)?
              Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
              [a-z\.]* match a single character present in the list below
              Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
              a-z a single character in the range between a and z (case sensitive)
              \. matches the character . literally
          4th Capturing group (\/[a-z1-9\/]*)*
              Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
             \/ matches the character / literally
              [a-z1-9\/]* match a single character present in the list below
                  Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
                  a-z a single character in the range between a and z (case sensitive)
                  1-9 a single character in the range between 1 and 9
                  \/ matches the character / literally
          \?? matches the character ? literally
              Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
          5th Capturing group ([\&a-z1-9=]*)?
              Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
             [\&a-z1-9=]* match a single character present in the list below
                  Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
                  \& matches the character & literally
                  a-z a single character in the range between a and z (case sensitive)
                  1-9 a single character in the range between 1 and 9
                  = the literal character =
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2015-03-26
        • 2011-07-30
        • 2012-02-04
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多