Regex 创建自定义正则表达式

Regex 创建自定义正则表达式,regex,Regex,如何创建可以检查以下内容的正则表达式: 可以有http或https 可能有,也可能没有 必须拥有steamcommunity.com 必须具有id或配置文件,后跟数字或文本 还应该说,如果它包含ID,它应该检查是否有文本,如果它包含配置文件,我应该检查数字。说明 给出你的示例文本 http://steamcommunity.com/id/rasmusvejby/ http://steamcommunity.com/profiles/76561198040893433 …这个正则表达式 ^h

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

可以有http或https 可能有,也可能没有 必须拥有steamcommunity.com 必须具有id或配置文件,后跟数字或文本

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

说明 给出你的示例文本

http://steamcommunity.com/id/rasmusvejby/
http://steamcommunity.com/profiles/76561198040893433
…这个正则表达式

^https?://(?:www\.)?steamcommunity\.com/(id/([^/\s]*)|profiles/([^/\s]*))
…将执行以下操作

验证url是否包含steamcommunity.com 匹配带或不带前导www 允许http或https 捕获url的id或配置文件部分 捕获id或配置文件的字符串 捕获组

组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
解释
@我不知道从哪里开始。这就是我想要的,如何开始。然后你应该在谷歌上搜索关于regex的教程,而不仅仅是让StackOverflow帮你完成工作。我们将帮助您解决特定的问题,但这里并不是简单地编写代码。
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