Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex——忽略到给定点的行_Regex_Key Value - Fatal编程技术网

Regex——忽略到给定点的行

Regex——忽略到给定点的行,regex,key-value,Regex,Key Value,我有一个正则表达式,可以处理部分数据。(与Perl兼容) 给定日志条目: pam_vas: Authentication <succeeded> for <active directory> user: <bobtheperson> account: <bobtheperson@com.com> reason: <N/A> Access cont(upn): <bob> 不幸的是,当我构建这个正则表达式时,我忽略了日志的一

我有一个正则表达式,可以处理部分数据。(与Perl兼容) 给定日志条目:

pam_vas: Authentication <succeeded> for <active directory> user: <bobtheperson> account: <bobtheperson@com.com> reason: <N/A> Access cont(upn): <bob>
不幸的是,当我构建这个正则表达式时,我忽略了日志的一个重要部分——第一部分。 日志实际上如下所示:

Feb 16 20:04:37 hostname su[1111]: [id 123456 auth.info] pam_vas: Authentication <succeeded> for <active directory> user: <bobtheperson> account: <bobtheperson@com.com> reason: <N/A> Access cont(upn): <bob>
Feb 16 20:04:37主机名su[1111]:[id 123456 auth.info]pam_vas:Authentication for)
如何从该日志文件中排除开始信息

**Feb 16 20:04:37 hostname su[1111]: [id 123456 auth.info]** pam_vas: Authentication <succeeded> for <active directory> user: <bobtheperson> account: <bobtheperson@com.com> reason: <N/A> Access cont(upn): <bob>
**Feb 16 20:04:37主机名su[1111]:[id 123456 auth.info]**pam\u vas:用户身份验证:帐户:原因:访问控制(upn):

我想我需要在最后一次出现a]:(就在pam_vas之前)之后开始搜索,但我不知道如何排除它。

您可以通过以下方法实现:

\b                 # a word boundary
(?P<key>[\w(): ]+) # the key part - word characters, (, ), :, spaces
\h+                # at least one whitespace (can be more)
<(?P<value>[^>]+)> # the value part in <> brackets
\b#单词边界
(?P[\w():]+)#关键部分-单词字符,(,),:,空格
\h+#至少有一个空格(可以更多)
]+)>#括号中的值部分

看。这样,就不需要忽略任何东西。

更新:误读了问题,解决这个问题的最佳方法似乎是

(?:^.*?pam_vas:)?\s+([^


我玩了几个变体,但发现这是最快的,捕获并忽略了日期戳

(?:^\*\*[^*]*\*\*[]pam\u vas:)?\s+([^

除非您使用的是带ignorewhitespace的内容,否则可以去掉单个空格周围的方括号。
[]

有较短的变体,但缺点是捕获太多,或采取许多步骤来实现,在500-800的数量级上,对于我发现的所有内容,这里是104

(?:              # Opens non-capturing group (ncg)
  ^              # ^ start of line, you may actually not want this
  \*\*           # Literally ** 
  [^*]*          # Anything but *, as many times as possible 
  \*\*           # Literally **
  [ ]            # A single space, only in brackets for visibility 
  pam_vas:       # Literally pam_vas: 
)                # Closes NCG
?                # Iterates NCG 0 or 1 times, thus "optional" 
\s+              # Any number of space characters, one or more
(                # Opens Capturing Group 1
  [^<:]*         # Any Character but < or :, as many times as possible 
)                # Closes CG1 
:?               # :, 0 or 1 times 
[ ]              # A single in space, only in brackets for visibility
<                # Literally <
(                # Opens CG2 
  [^>]*          # Any character but >, as many times as possible 
)                # Closes CG2
>                # Literally >
(?:#打开非捕获组(ncg)
^#^从这一行开始,你可能实际上并不想要这个
\*\*#字面意思**
尽可能多地做除*以外的任何事情
\*\*#字面意思**
[]#单个空格,仅在括号中表示可见性
帕姆瓦斯:字面上的帕姆瓦斯:
)#关闭NCG
?迭代NCG 0或1次,因此为“可选”
\s+#任意数量的空格字符,一个或多个
(#打开捕获组1
[^]*#除>以外的任何字符,尽可能多次
)#关闭CG2
>#字面意思>

在Splunk论坛上与某人交谈后,我有一个正则表达式:

\s+([^\:\<\>]+)(?:\:?\s\<)([^\>]+)\>
\s+([^\:\]+)(?:\:?\s\]+)\>

[\>\:]
最好写成
[>:]
[>:]*
在这方面没有效果。甚至可以写成我希望在工作时加载regex101的人……这似乎是一个很酷的资源。在我的示例中,我“加粗”了我想要排除的文本。不幸的是,它似乎只放了双“*”不管怎么说,它代替了粗体.Odd.Anyways——这正是我要找的内容。@izzmit啊,我错过了它——它这样做的原因是因为它是一个代码框的一部分。@izzmit是一行/文件开头的日期?是的。它们是syslog,所以每一行代表一个新的日志条目。我将在splunk中使用它来解析一些日志数据laris BSM身份验证日志。
\s+([^\:\<\>]+)(?:\:?\s\<)([^\>]+)\>