检索给定搜索条件的字符串。括号、引语等
Retrieve string given search criteria. Brackets, quotations and more
我有以下字符串:
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"
我需要能够检索句子 "I'm good, how are you?"。我完全被难住了。
我试图在 "Text [str] =" 之后检索短语,它返回了我需要的内容。但它也会返回 "Text [str] =".
之后的所有其他句子
PartyId [int] 字段可能对你们有所帮助。 1对应客户端。哪个是我需要的人的留言。
我只是不知道如何缩小范围。
请帮忙!
描述
^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:).)*
** 要更好地查看图像,只需右键单击图像并 select 在新 window
中查看
例子
现场演示
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 在第一个
NoticeText
中获取 EventInfo
之后的第一个 Text [str]
- 捕获组 2 在第二个
NoticeText
中获得 PartyID
之后的第二个 Text [str]
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 :
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
(?: 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 :
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
(?: 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])*
在这个版本中,我使用了全局标志和多行标志
例子
使用上面的相同示例文本,捕获组 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?`
现场演示
我有以下字符串:
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"
我需要能够检索句子 "I'm good, how are you?"。我完全被难住了。
我试图在 "Text [str] =" 之后检索短语,它返回了我需要的内容。但它也会返回 "Text [str] =".
之后的所有其他句子PartyId [int] 字段可能对你们有所帮助。 1对应客户端。哪个是我需要的人的留言。
我只是不知道如何缩小范围。
请帮忙!
描述
^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:).)*
** 要更好地查看图像,只需右键单击图像并 select 在新 window
中查看例子
现场演示
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 在第一个
NoticeText
中获取 - 捕获组 2 在第二个
NoticeText
中获得
EventInfo
之后的第一个 Text [str]
PartyID
之后的第二个 Text [str]
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 :
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
(?: 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 :
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
(?: 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])*
在这个版本中,我使用了全局标志和多行标志
例子
使用上面的相同示例文本,捕获组 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?`
现场演示