2016-06-27 15 views
1

Ich habe die folgende Zeichenfolge: „Ich bin gut, wie du bist“Abrufen der angegebenen Suchkriterien. Klammern, Angebote und mehr

NoticeText: 
    NoticeType [str] = USER_TYPING_ON 
    Text [str] = "user is typing" 
    EventInfo: 
     PartyId [int] = 2 
     EventType [str] = MESSAGE 
     UserNickname [str] = "Michael" 
     EventId [int] = 4 
     Text [str] = "Hey, how are you?" 
     MsgCheck [str] = NONE 
     TimeOffset [int] = 23 
     UserType [str] = AGENT 
NoticeText: 
    NoticeType [str] = USER_TYPING_ON 
    EventInfo: 
    PartyId [int] = 1 
     EventType [str] = MESSAGE 
     UserNickname [str] = "Bob Smith" 
     EventId [int] = 6 
     Text [str] = "I'm good, how are you?" 
     MsgCheck [str] = NONE 
     TimeOffset [int] = 28 
     UserType [str] = CLIENT 
     MessageType [str] = "text" 

Ich muss in der Lage, den Satz zu holen. Ich bin völlig ratlos.

Ich habe versucht, Sätze nach "Text [str] =" abrufen, das gibt zurück, was ich brauche. Aber es gibt auch alle anderen Sätze nach "Text [str] =" zurück.

Ein Tipp, der Ihnen helfen könnte, ist das PartyId [int], Feld. 1 entspricht dem Client. Welches ist die Botschaft der Person, die ich brauche?

Ich weiß nur nicht, wie man es dadurch eingrenzen kann.

Bitte helfen!

Antwort

0

Beschreibung

^NoticeText:(?:(?!\nNoticeText:).)*\n\s+EventInfo(?:(?!\nNoticeText:).)*\n\s+Text\s*\[str\]\s*=\s*"([^"]*)"(?:(?!\nNoticeText:).)*\nNoticeText:(?:(?!\nNoticeText:).)*\n\s+EventInfo(?:(?!\nNoticeText:).)*\n\s+Text\s*\[str\]\s*=\s*"([^"]*)"(?:(?!\nNoticeText:).)*

Regular expression visualization

** Um das Bild besser zu sehen, einfach mit der rechten klicken Sie auf das Bild und wählen Sie Ansicht in einem neuen Fenster

Beispiel

Live-Demo

https://regex101.com/r/tD6uV9/1

Beispieltext

NoticeText: 
    NoticeType [str] = USER_TYPING_ON 
    Text [str] = "user is typing" 
    EventInfo: 
     PartyId [int] = 2 
     EventType [str] = MESSAGE 
     UserNickname [str] = "Michael" 
     EventId [int] = 4 
     Text [str] = "Hey, how are you?" 
     MsgCheck [str] = NONE 
     TimeOffset [int] = 23 
     UserType [str] = AGENT 
NoticeText: 
    NoticeType [str] = USER_TYPING_ON 
    EventInfo: 
    PartyId [int] = 1 
     EventType [str] = MESSAGE 
     UserNickname [str] = "Bob Smith" 
     EventId [int] = 6 
     Text [str] = "I'm good, how are you?" 
     MsgCheck [str] = NONE 
     TimeOffset [int] = 28 
     UserType [str] = CLIENT 
     MessageType [str] = "text" 

Probe Spiele

  • Capture-Gruppe 0 die beiden NoticeText Blöcke
  • Capture-Gruppe 1 erhält der erste bekommt Text [str] nachdem die EventInfo im ersten NoticeText
  • Capture-Gruppe 2 erhält die zweiten Text [str] nach PartyID in der zweiten NoticeText
MATCH 1 
Capture Group 1. [246-263] `Hey, how are you?` 
Capture Group 2. [566-588] `I'm good, how are you?` 

Erläuterung

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of the string 
---------------------------------------------------------------------- 
    NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    \n      '\n' (newline) 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    EventInfo    'EventInfo' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    \n      '\n' (newline) 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    Text      'Text' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    str      'str' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    =      '=' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [^"]*     any character except: '"' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    \n      '\n' (newline) 
---------------------------------------------------------------------- 
    NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    \n      '\n' (newline) 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    EventInfo    'EventInfo' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    \n      '\n' (newline) 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    Text      'Text' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    str      'str' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    =      '=' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [^"]*     any character except: '"' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     \n      '\n' (newline) 
---------------------------------------------------------------------- 
     NoticeText:    'NoticeText:' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    .      any character except \n 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 

Alternativ

Wenn Sie eine lange Liste dieser NoticeText Blöcke hätten, könnten Sie sie alle mit dieser vereinfachten Version des gleichen Ausdrucks analysieren.

^NoticeText:(?:(?!\nNoticeText:)[\s\S])*\n\s+Text\s*\[str\]\s*=\s*"([^"]*)"(?:(?!\nNoticeText:)[\s\S])*

Regular expression visualization

Mit dieser Version ich die globalen Flag bin mit und dem Multiline-Flag

Beispiel

Mit dem gleichen Beispieltext von oben Capture-Gruppe 0 wird die Single NoticeText, und Erfassungsgruppe 1 erhält nur den letzten Text [str] Wert im Block

Probe Spiele

MATCH 1 
Capture Group 1. [246-263] `Hey, how are you?` 

MATCH 2 
Capture Group 1. [566-588] `I'm good, how are you?` 

Live Demo

https://regex101.com/r/uW6cV6/1