创建自定义正则表达式

时间:2016-05-03 23:47:32

标签: regex

如何创建可以检查以下内容的正则表达式:

还应该说,如果它包含ID,它应该检查是否有文本,如果它同意配置文件,我应该检查数字。

1 个答案:

答案 0 :(得分:1)

能解密

鉴于您的示例文本......

http://steamcommunity.com/id/rasmusvejby/
http://steamcommunity.com/profiles/76561198040893433

......这个正则表达式......

^https?://(?:www\.)?steamcommunity\.com/(id/([^/\s]*)|profiles/([^/\s]*))

...将执行以下操作

  1. 验证网址包含steamcommunity.com
  2. 与前导www
  3. 匹配或不匹配
  4. 允许httphttps
  5. 捕获网址的ID或个人资料部分
  6. 捕获id或个人资料的字符串
  7. 捕获群组

    • 组0获取完整字符串
    • 第1组获取(ID或配置文件)和关联值
    • 第2组只获取ID
    • 的值
    • 第3组只获取配置文件的值

    实施例

    示例匹配

    [0][0] = http://steamcommunity.com/id/rasmusvejby
    [0][1] = id/rasmusvejby
    [0][2] = rasmusvejby
    [0][3] = 
    
    [1][0] = http://steamcommunity.com/profiles/76561198040893433
    [1][1] = profiles/76561198040893433
    [1][2] = 
    [1][3] = 76561198040893433
    

    解释

    Regular expression visualization

    NODE                     EXPLANATION
    ----------------------------------------------------------------------
      ^                        the beginning of a "line"
    ----------------------------------------------------------------------
      http                    'http'
    ----------------------------------------------------------------------
      s?                       with or without 's'
    ----------------------------------------------------------------------
      ://                      '://'
    ----------------------------------------------------------------------
      (?:                      group, but do not capture (optional
                               (matching the most amount possible)):
    ----------------------------------------------------------------------
        www                      'www'
    ----------------------------------------------------------------------
        \.                       '.'
    ----------------------------------------------------------------------
      )?                       end of grouping
    ----------------------------------------------------------------------
      steamcommunity           'steamcommunity'
    ----------------------------------------------------------------------
      \.                       '.'
    ----------------------------------------------------------------------
      com/                     'com/'
    ----------------------------------------------------------------------
      (                        group and capture to \1:
    ----------------------------------------------------------------------
        id/                      'id/'
    ----------------------------------------------------------------------
        (                        group and capture to \2:
    ----------------------------------------------------------------------
          [^/\s]*                  any character except: '/', whitespace
                                   (\n, \r, \t, \f, and " ") (0 or more
                                   times (matching the most amount
                                   possible))
    ----------------------------------------------------------------------
        )                        end of \2
    ----------------------------------------------------------------------
       |                        OR
    ----------------------------------------------------------------------
        profiles/                'profiles/'
    ----------------------------------------------------------------------
        (                        group and capture to \3:
    ----------------------------------------------------------------------
          [^/\s]*                  any character except: '/', whitespace
                                   (\n, \r, \t, \f, and " ") (0 or more
                                   times (matching the most amount
                                   possible))
    ----------------------------------------------------------------------
        )                        end of \3
    ----------------------------------------------------------------------
      )                        end of \1