2016-06-27 2 views
1

私は次の文字列があります。検索条件が指定された文字列を取得します。ブラケット、引用、より

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" 

を、私は、文取得できるようにする必要があり、「私はいいんだが、お元気ですか?」。私は完全に困惑しています。

"Text [str] ="の後にフレーズを検索しようとしましたが、必要なものが返されました。しかし、 "Text [str] ="の後に他の文もすべて戻します。

PartyId [int]フィールドには、あなたを助けるヒントが1つあります。 1はクライアントに対応します。それは私が必要とする人のメッセージです。

私はちょうどその方法でそれを絞り込む方法がわかりません。

助けてください!

答えて

0

説明

^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

**単に右新しいウィンドウで画像を選択し表示をクリックし

、良好な画像を表示するには例

ライブデモ

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

サンプルテキスト

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" 

サンプル

  • キャプチャグループ0の両方NoticeTextブロック
  • キャプチャグループ1は、第1の取得取得一致Text [str]EventInfo
  • NoticeTextでキャプチャグループ2は第 NoticeText
MATCH 1 
Capture Group 1. [246-263] `Hey, how are you?` 
Capture Group 2. [566-588] `I'm good, how are you?` 

説明

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 
---------------------------------------------------------------------- 

あるいは

PartyID後に第2 Text [str]を取得した後に

これらのブロックのリストが長い場合は、同じ表現のこの単純化されたバージョンですべてを解析することができます。私はグローバルフラグと複数行フラグ

例を使用しています。このバージョンでは

Regular expression visualization

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

キャプチャグループ0上記から同じサンプルテキスト付き

は、単一の取得しますNoticeTextであり、キャプチャ・グループ1はブロック内の最後のText [str]値のみを取得します

サンプル

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

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

ライブデモ

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

にマッチします
関連する問題