[Solved] Create a custom regex


Decription

Given your sample text…

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

…this Regex…

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

…will do the following

  1. validate the url contains steamcommunity.com
  2. matches with or without the leading www
  3. Allows http or https
  4. captures the id or profile portion of the url
  5. captures the string for the id or profile

Capture Groups

  • Group 0 gets the full string
  • Group 1 gets the (ID or Profiles) and the associated value
  • Group 2 gets just the value of the ID
  • Group 3 gets just the value of the Profile

Example

Sample matches

[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

Explanation

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: "https://stackoverflow.com/", 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: "https://stackoverflow.com/", whitespace
                               (\n, \r, \t, \f, and " ") (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of \3
----------------------------------------------------------------------
  )                        end of \1

solved Create a custom regex