Regex 匹配单词和最后出现的空格之间的顺序
我希望提取特定单词后的特定字符集,直到序列中出现的最后一个空格 例如:Regex 匹配单词和最后出现的空格之间的顺序,regex,regex-negation,regex-greedy,regex-lookarounds,Regex,Regex Negation,Regex Greedy,Regex Lookarounds,我希望提取特定单词后的特定字符集,直到序列中出现的最后一个空格 例如: FAILED on portal HTTP (10.1.1.1) FAILED on portal TELNET 0 SSH (10.1.1.1) 我希望O/p为: HTTP TELNET 0 SSH 当前正在使用以下正则表达式并正在使用它: .+((?<=portal)[^\s]]+ 正则表达式: ^(\d+).* (\S+\d) ([\w\s]+) (\w* ?AUTHENTICATION:SESSION)
FAILED on portal HTTP (10.1.1.1)
FAILED on portal TELNET 0 SSH (10.1.1.1)
我希望O/p为:
HTTP
TELNET 0 SSH
当前正在使用以下正则表达式并正在使用它:
.+((?<=portal)[^\s]]+
正则表达式:
^(\d+).* (\S+\d) ([\w\s]+) (\w* ?AUTHENTICATION:SESSION) (.+) (([\w.]+):(\d+)).*
通常,我希望从示例字符串中获得的组是:
#1 - 1368028793000
#2 - 10.3.1.4
#3 - CISCO X
#4 - AUTHENTICATION:SESSION
#5 - User authentication attempt FAILED on portal
#6 - TELNET 0 SSH
#7 - 10.1.2.8
#8 - 6940
你可以使用这个正则表达式
(?<=portal).+(?=\s)
(?根据新要求进行所有更改
尝试一下:
^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$
下面是一个运行它的perl脚本:
my $re = qr/^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$/;
while(<DATA>) {
chomp;
my @l = ($_ =~ $re);
dump@l;
}
__DATA__
1368028793000 10.3.1.4 CISCO X AUTHENTICATION:SESSION User authentication attempt FAILED on portal HTTP (10.1.1.1)
1368028793000 10.3.1.4 CISCO X AUTHENTICATION:SESSION User authentication attempt FAILED on portal TELNET 0 SSH (10.1.2.8:64940)
正则表达式解释:
(
1368028793000,
"10.3.1.4",
"CISCO X",
"AUTHENTICATION:SESSION",
"User authentication attempt FAILED on portal",
"HTTP ",
"10.1.1.1",
undef,
)
(
1368028793000,
"10.3.1.4",
"CISCO X",
"AUTHENTICATION:SESSION",
"User authentication attempt FAILED on portal",
"TELNET 0 SSH ",
"10.1.2.8",
64940,
)
The regular expression:
(?-imsx:^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[\d.]+ any character of: digits (0-9), '.' (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[\w\s]+? any character of: word characters (a-z,
A-Z, 0-9, _), whitespace (\n, \r, \t,
\f, and " ") (1 or more times (matching
the least amount possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
AUTHENTICATION:SES 'AUTHENTICATION:SESSION'
SION
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
\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 (1 or more times
(matching the least amount possible))
----------------------------------------------------------------------
portal 'portal'
----------------------------------------------------------------------
) end of \5
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
( group and capture to \6:
----------------------------------------------------------------------
.+? any character except \n (1 or more times
(matching the least amount possible))
----------------------------------------------------------------------
) end of \6
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
( group and capture to \7:
----------------------------------------------------------------------
[\d.]+ any character of: digits (0-9), '.' (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \7
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
( group and capture to \8:
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of \8
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
你可以试试这个:
(?<=portal\s)(.+)\s\(
(?谢谢..但是这一个给出了下面的O/P门户TELNET 0 SSH(和)TELNET 0SSH@Designerztouch:对不起,我没有收到你的信。好的。我已经把我所需要的东西从什么地方放进去了:)我希望现在一切都清楚了。谢谢!老兄!这对我来说是100%有效的!:D非常感谢你及时和巨大的帮助。你救了我一天…好的。我将描述我正在尝试的全部内容文本:1368028793000 10.3.1.4 CISCO X身份验证:在门户TELNET 0 SSH(10.1.2.8:64940)Regex:^(\D+)*上会话用户身份验证尝试失败(\S++\d)([\w\S]+)(\w*?身份验证:会话)(.+)([\w.]+):(\d+)。*您真的想捕获所有这些组吗?还是只捕获门户之后的组?我已成功捕获了所有组…但我想捕获单独组中“门户”之后的组。可能是HTTP、TELNET 0 SSH、FTP等。您能否根据示例字符串解释要保留哪些组及其值?
(?<=portal\s)(.+)\s\(