regex 创建自定义正则表达式

juzqafwq  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(115)

如何创建一个正则表达式来检查以下内容:

  • 可以有httphttps
  • 可能有www或没有
  • 必须有steamcommunity.com
  • 必须有idprofiles后跟数字或文本

http://steamcommunity.com/id/rasmusvejby/http://steamcommunity.com/profiles/76561198040893433
还应该说,如果它包含ID,它应该检查是否有文本,如果它包含配置文件,我应该检查数字。

fbcarpbf

fbcarpbf1#

描述

鉴于您的样本文本...

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

这个Regex

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

。会做以下事情
1.验证url是否包含steamcommunity.com
1.匹配有或没有前导www
1.允许httphttps
1.捕获url的id或profile部分
1.捕获id或配置文件的字符串
捕捉组

  • 组0获取完整字符串
  • 组1获取(ID或Profiles)和关联值
  • 组2只获取ID的值
  • 第3组仅获取Profile的值

示例

样本匹配

[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

说明

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

相关问题