2017-09-26 1 views
0

Ich habe eine Zeichenfolge wie dieseRegex Spiel Alles bis zu entfernen nachstehendes Leerzeichen

IF condition EQUALS value THEN do this

Ich möchte ohne Anfang, den Zustand erhalten oder Leerzeichen. Das ist, was ich versucht:

/\b(IF)\b[\w\d\s]+?(?=EQUALS)(EQUALS)/g

, die fast bis auf den Zustand der arbeitet, hat Leerzeichen vor und nach ihm.

Sehen Sie ein vollständiges Beispiel hier: https://regex101.com/r/qsjlia/4

+0

'\ b (IF) \ b \ s + ([\ w \ d \ s] +?) \ s + (EQUALS) '? – ctwheels

+0

Ich versuche, bei regulären Ausdrücken besser zu werden, und möchte dies als Teil der – Jordash

+0

Siehe https://regex101.com/r/D6PAFT/1 –

Antwort

1

Sie können unter regex versuchen:

(?:^IF\s+)(\w+)(?=\s+EQUALS\s) 

DEMO

1

In dieser vorgeschlagenen Lösung, die ich es ein bisschen weiter durch die Annahme getroffen haben, dass bei einigen Punkt, den Sie haben möchten Bedingung, Entspricht Klausel, Wert und sowohl die Dann & Else

Beschreibung

Diese Regex

IF\s+((?:(?!\s(?:NOT)?EQUALS\s).)*)\s+((?:NOT)?EQUALS)\s+((?:(?!\sTHEN\s).)*)\s+THEN\s+((?:(?!\sELSE\s).)*)\s+ELSE\s+(.*)

enter image description here

** Um besser das Bild zu sehen, klicken Sie einfach rechts die Bild und wählen Sie Ansicht in n ew Fenster

Wird wie folgt vor:

  • Ziehen Sie den Zustand, Equal-Klausel, Wert, dann und Else-Sätze
  • Shop die erfassten Zeichen in nummerierte Capture-Gruppen.
  • ignorieren weißen Raum vor und nach jedem Stück Text

Beispiel

Sehen Sie diese live demo auch.

Ihr Beispieltext

Gegeben
IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28 
IF factor EQUALS value THEN do this ELSE do something else. 
IF factor NOTEQUALS value THEN do this ELSE do something else. 
IF 2 NOTEQUALS 'Droids' THEN Not the droids you are looking for ELSE Found the droids 

Die Regex folgende Capture Gruppen erstellen:

  • Capture-Gruppe 0 wird die gesamte Zeile
  • Capture-Gruppe 1 erhält die Condition
  • erhalten
  • Fanggruppe 2 erhält die EQUALS oder NOTEQUALS
  • Capture-Gruppe 3 die Value
  • Capture-Gruppe 4 den Then Block
  • Capture-Gruppe 5 erhält den Else Block

Mit den folgenden Spielen erhalten erhalten:

[0][0] = IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28 
[0][1] = 45 
[0][2] = EQUALS 
[0][3] = 'Yes' 
[0][4] = ASSIGN 28 
[0][5] = SHOW 28 

[1][0] = IF factor EQUALS value THEN do this ELSE do something else. 
[1][1] = factor 
[1][2] = EQUALS 
[1][3] = value 
[1][4] = do this 
[1][5] = do something else. 

[2][0] = IF factor NOTEQUALS value THEN do this ELSE do something else. 
[2][1] = factor 
[2][2] = NOTEQUALS 
[2][3] = value 
[2][4] = do this 
[2][5] = do something else. 

[3][0] = IF 2 NOTEQUALS 'Droids' THEN Found the droids ELSE not the droids you are looking for 
[3][1] = 2 
[3][2] = NOTEQUALS 
[3][3] = 'Droids' 
[3][4] = Not the droids you are looking for 
[3][5] = Found the droids 

Kurz Erläuterung

Jede der Erfassungsgruppen verwendet dies ein erweitertes Regex-Konstrukt zum Erfassen, bis es auf einen bekannten Zeichenblock trifft.

Aus dem obigen Ausdruck eines dieser Konstrukte ist ((?:(?!\sTHEN\s).)*)\s+THEN\s+

NODE      EXPLANATION 
(      group and capture 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
     THEN      'THEN' 
---------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
     .      any character except \n 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
)      end of capture group 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    THEN      'THEN' 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 

Detaillierte Erläuterung

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
    IF      'IF' 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
     (?:      group, but do not capture (optional 
           (matching the most amount 
           possible)): 
-------------------------------------------------------------------------------- 
      NOT      'NOT' 
-------------------------------------------------------------------------------- 
     )?      end of grouping 
-------------------------------------------------------------------------------- 
     EQUALS     'EQUALS' 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
     .      any character except \n 
-------------------------------------------------------------------------------- 
    )*      end of grouping 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \2: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
-------------------------------------------------------------------------------- 
     NOT      'NOT' 
-------------------------------------------------------------------------------- 
    )?      end of grouping 
-------------------------------------------------------------------------------- 
    EQUALS     'EQUALS' 
-------------------------------------------------------------------------------- 
)      end of \2 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \3: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
     THEN      'THEN' 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
     .      any character except \n 
-------------------------------------------------------------------------------- 
    )*      end of grouping 
-------------------------------------------------------------------------------- 
)      end of \3 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    THEN      'THEN' 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \4: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
     ELSE      'ELSE' 
-------------------------------------------------------------------------------- 
     \s      whitespace (\n, \r, \t, \f, and " ") 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
     .      any character except \n 
-------------------------------------------------------------------------------- 
    )*      end of grouping 
-------------------------------------------------------------------------------- 
)      end of \4 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    ELSE      'ELSE' 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \5: 
-------------------------------------------------------------------------------- 
    .*      any character except \n (0 or more times 
          (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
)      end of \5 
Verwandte Themen