Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

Create a custom regex

How can i create a regex that can check for the following:

Also it should be said that if it contains ID it should check if there are text, and if it conntainns profiles i should check for numbers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: '/', whitespace
                               (
, 
, 	, 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
                               (
, 
, 	, f, and " ") (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of 3
----------------------------------------------------------------------
  )                        end of 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...