2

我正在尝试使用正则表达式解决字符串匹配问题。我需要匹配这种形式的 URL:

http://soundcloud.com/okapi23/dont-turn-your-back/

我需要“拒绝”这种形式的 URL:

http://soundcloud.com/okapi23/sets/happily-reversed/

尾随的“/”显然是可选的。

所以基本上:

  • 在主机名之后,可以有 2 或 3 个组,如果在第二个中等于 "sets",则正则表达式不应匹配。
  • "sets"可以包含在 URL 中的任何其他位置
  • "sets"需要完全匹配

到目前为止,我想出的是http(s)?://(www\.)?soundcloud\.com/.+/(?!sets)\b(/.+)?,失败了。

有什么建议么?是否有任何库可以简化任务(例如,使斜杠可选)?

4

3 回答 3

5

假设 OP 想要测试给定的字符串是否包含满足以下要求的 URL:

  • URL 方案必须是http:https:
  • URL 权限必须是//soundcloud.com//www.soundcloud.com
  • URL 路径必须存在并且必须包含 2 或 3 个路径段。
  • 第二个路径段不能是:"sets"
  • 每个路径段必须由一个或多个仅由字母数字字符 ( [A-Za-z0-9]) 组成的“单词”组成,并且多个单词由一个短划线或下划线分隔。
  • URL 必须没有查询或片段组件。
  • URL 路径可能以可选的"/".
  • URL 应该不区分大小写。

这是一个经过测试的 JavaScript 函数(带有完全注释的正则表达式),它可以解决问题:

function isValidCustomUrl(text) {
    /* Here is the regex commented in free-spacing mode:
    # Match specific URL having non-"sets" 2nd path segment.
    ^                          # Anchor to start of string.
    https?:                    # URL Scheme (http or https).
    //                         # Begin URL Authority.
    (?:www\.)?                 # Optional www subdomain.
    soundcloud\.com            # URL DNS domain.
    /                          # 1st path segment (can be: "sets").
    [A-Za-z0-9]+               # 1st word-portion (required).
    (?:                        # Zero or more extra word portions.
      [-_]                     # only if separated by one - or _.
      [A-Za-z0-9]+             # Additional word-portion.
    )*                         # Zero or more extra word portions.
    (?!/sets(?:/|$))           # Assert 2nd segment not "sets".
    (?:                        # 2nd and 3rd path segments.
      /                        # Additional path segment.
      [A-Za-z0-9]+             # 1st word-portion.
      (?:                      # Zero or more extra word portions.
        [-_]                   # only if separated by one - or _.
        [A-Za-z0-9]+           # Additional word-portion.
      )*                       # Zero or more extra word portions.
    ){1,2}                     # 2nd path segment required, 3rd optional.
    /?                         # URL may end with optional /.
    $                          # Anchor to end of string.
    */
    // Same regex in javascript syntax:
    var re = /^https?:\/\/(?:www\.)?soundcloud\.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?$/i;
    if (re.test(text)) return true;
    return false;
}
于 2012-10-23T15:32:42.803 回答
4

而不是.使用[a-zA-Z][\w-]* 这意味着“匹配一个字母后跟任意数量的字母、数字、下划线或连字符”。

^https?://(www\.)?soundcloud\.com/[a-zA-Z][\w-]*/(?!sets(/|$))[a-zA-Z][\ w-]*(/[a-zA-Z][\w-]*)?/?$

要获取可选的尾部斜杠,请使用/?$.

在 Javascript 正则表达式文字中,所有正斜杠都必须转义。

于 2012-10-23T13:57:47.693 回答
1

我建议你使用正则表达式模式

^https?:\/\/soundcloud\.com(?!\/[^\/]+\/sets(?:\/|$))(?:\/[^\/]+){2,3}\/?$
于 2012-10-23T14:17:35.937 回答