检索给定搜索条件的字符串。括号,报价等

时间:2016-06-27 14:31:21

标签: java regex string substring phrase

我有以下字符串:

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 :(得分: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获得第一个Text [str]
  • EventInfo后的第一个NoticeText
  • 在第二个Text [str]
  • PartyID之后,第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
----------------------------------------------------------------------

替代地

如果您有这些NoticeText块的长列表,那么您可以使用相同表达式的简化版本解析它们。

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

Regular expression visualization

使用此版本我使用全局标志和多行标志

实施例

使用上面相同的示例文本,Capture Group 0获取单个NoticeText,Capture组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