2016-05-24 40 views
0

我有一个关于正则表达式来从URL获取信息的问题。如何使用Regx获取Subsite或QueryString

它可能会在前面讨论,但我正在寻找一种混合方法。

如果用户要么提供了一个子网站,要么用户提供了一个查询字符串,并且根据条件我想在URL请求中添加一个规则。

正则表达式:/([^,]*)
输入:youtube.com/data/beta

我得到数据/测试,这正是我所期待的。

但是当我通过输入为http://youtube.com/data/beta,它给我/youtube..../,这是正确的,但我想排除第一//[DomainName]

注:我不能排除在youtube.com,因为我打算在某些规则中使用这个正则表达式,所以请给我发送回答或评论,它可以适用于任何类型的URL。

回答

0

说明

^(?:https?:\/\/)?[^\/]+\/|([^?\n]+) 

Regular expression visualization

这个正则表达式将执行以下操作:

  • 匹配字符串开始http://https://
  • 跳过域名
  • 捕捉的子域名之后和查询字符串之前

现场演示

https://regex101.com/r/zC4gZ6/1

示例文本

youtube.com/data/beta 
http://youtube.com/data/beta?Droid=This_is_not_the_droid_you_are_looking_for 

样品匹配

[1][0] = youtube.com/data/beta 
[1][1] = data/beta 

[2][0] = http://youtube.com/data/beta 
[2][1] = data/beta 

说明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of a "line" 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    http      'http' 
---------------------------------------------------------------------- 
    s?      's' (optional (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    [^\/]+     any character except: '\/' (1 or more 
          times (matching the most amount possible)) 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [^?\n]+     any character except: '?', '\n' 
          (newline) (1 or more times (matching the 
          most amount possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 

加分

要包括查询字符串,如果他们存在,那么添加(?:\?(.*?))?$ 上述表达式的末尾,以便它看起来像这样。

^(?:https?:\/\/)?[^\/]+\/([^?\n]+)(?:\?(.*?))?$ 

Regular expression visualization

+0

感谢滚装哟,你真棒。但我只有一个问题,我怎样才能将查询字符串也包含在结果中? –

+0

我刚在我的答案的末尾提供了一个更新来覆盖查询字符串部分。这允许查询字符串部分存在或不存在。 –

相关问题