2016-05-03 3 views
-5

Wie kann ich einen regulären Ausdruck erstellen, die für das folgende überprüfen:eine benutzerdefinierte regex

Auch sollte man sagen, dass, wenn es ID enthält, sollte es prüfen, ob es Text gibt, und wenn es Profile conntainns i für Zahlen überprüfen soll.

+0

@ nhouser9 Ich weiß nicht, wo ich anfangen soll. Das ist ziemlich viel, wonach ich suche, wie ich anfangen soll. –

+2

Dann sollten Sie auf Google für Tutorials auf Regex suchen, nicht nur StackOverflow bitten, Ihre Arbeit für Sie zu erledigen.Wir helfen Ihnen bei bestimmten Problemen, sind aber nicht dazu da, Ihren Code einfach zu schreiben. – nhouser9

Antwort

1

Decription

Ihr Beispieltext gegeben ...

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

... diese Regex ...

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

... wird die folgenden

    tun
  1. validieren Sie die URL enthält steamcommunity.com
  2. Matches mit oder ohne die führende www
  3. Ermöglicht http oder https
  4. fängt die ID oder Profil-Teil der URL
  5. fängt die Zeichenfolge für die ID oder Profil

Capture-Gruppen

  • Gruppe 0 erhält die vollständige Zeichenfolge
  • Gruppe 1 erhält die (ID oder Profile) und den zugehörigen Wert
  • Gruppe 2
  • Gruppe 3 erhält nur den Wert der ID wird nur den Wert des Profils

Beispiel

Beispielspiele

[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 

Erklärung

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