Filebeat对正则表达式的支持

Filebeat正则表达式的支持是基于RE2的,本文译自 elastic

Filebeat有几个接受正则表达式的配置选项。例如multiline.pattern, include_linesexclude_lines,和 exclude_files所有接受正则表达式。

注意:建议正则放在单引号内,例如'^\[?[0-9][0-9]:?[0-9][0-9]|^[[:graph:]]+'

样例描述

单个字符

 

x

单个字符

.

任何字符

[xyz]

字符类

[^xyz]

非字符类

[[:alpha:]]

ASCII字符类

[[:^alpha:]]

非ASCII字符类

\d

Perl字符类

\D

非Perl字符类

\pN

Unicode字符类(一个字母的名称)

\p{Greek}

Unicode字符类

\PN

非Unicode字符类(一个字母的名称)

\P{Greek}

非Unicode字符类

复合类型

 

xy

x|y

重复类型

 

x*

以x开头

x+

一个或者多个x

x?

零或一个x

x{n,m}

n or n+1 or … or m x, prefer more

x{n,}

n or more x, prefer more

x{n}

exactly n x

x*?

zero or more x, prefer fewer

x+?

one or more x, prefer fewer

x??

zero or one x, prefer zero

x{n,m}?

n or n+1 or … or m x, prefer fewer

x{n,}?

n or more x, prefer fewer

x{n}?

exactly n x

分组

 

(re)

numbered capturing group (submatch)

(?P<name>re)

named & numbered capturing group (submatch)

(?:re)

non-capturing group

(?i)abc

set flags within current group, non-capturing

(?i:re)

set flags during re, non-capturing

(?i)PaTTeRN

case-insensitive (default false)

(?m)multiline

multi-line mode: ^ and $ match begin/end line in addition to begin/end text (default false)

(?s)pattern.

let . match \n (default false)

(?U)x*abc

ungreedy: swap meaning of x* and x*?x+ and x+?, etc (default false)

空字符串

 

^

at beginning of text or line (m=true)

$

at end of text (like \z not \Z) or line (m=true)

\A

at beginning of text

\b

at ASCII word boundary (\w on one side and \W\A, or \z on the other)

\B

not at ASCII word boundary

\z

at end of text

转义序列

 

\a

bell (same as \007)

\f

form feed (same as \014)

\t

horizontal tab (same as \011)

\n

newline (same as \012)

\r

carriage return (same as \015)

\v

vertical tab character (same as \013)

\*

literal *, for any punctuation character *

\123

octal character code (up to three digits)

\x7F

two-digit hex character code

\x{10FFFF}

hex character code

\Q...\E

literal text ... even if ... has punctuation

ASCII字符类

 

[[:alnum:]]

alphanumeric (same as [0-9A-Za-z])

[[:alpha:]]

alphabetic (same as [A-Za-z])

[[:ascii:]]

ASCII (same as \x00-\x7F])

[[:blank:]]

blank (same as [\t ])

[[:cntrl:]]

control (same as [\x00-\x1F\x7F])

[[:digit:]]

digits (same as [0-9])

[[:graph:]]

graphical (same as [!-~] == [A-Za-z0-9!"#$%&'()*+,\-./:;<=>?@[\\\]^_` {|}~])

[[:lower:]]

lower case (same as [a-z])

[[:print:]]

printable (same as [ -~] == [ [:graph:]])

[[:punct:]]

punctuation (same as [!-/:-@[-`{-~])

[[:space:]]

whitespace (same as [\t\n\v\f\r ])

[[:upper:]]

upper case (same as [A-Z])

[[:word:]]

word characters (same as [0-9A-Za-z_])

[[:xdigit:]]

hex digit (same as [0-9A-Fa-f])

支持Perl字符类

 

\d

digits (same as [0-9])

\D

not digits (same as [^0-9])

\s

whitespace (same as [\t\n\f\r ])

\S

not whitespace (same as [^\t\n\f\r ])

\w

word characters (same as [0-9A-Za-z_])

\W

not word characters (same as [^0-9A-Za-z_])

 

实际使用整理,待续...

转载于:https://my.oschina.net/openplus/blog/1591317

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
filebeat中的正则表达式可以用于对日志文件进行过滤和解析。filebeat支持多种正则表达式语法,例如PCRE、Golang和Oniguruma等。在filebeat中,常见的正则表达式应用包括: 1. 在输入文件路径中使用正则表达式,以匹配多个文件; 2. 在filebeat配置文件中使用正则表达式,以从日志中提取特定字段; 3. 在filebeat处理器中使用正则表达式,以将字段重命名或删除。 下面是一个使用正则表达式从Nginx日志中提取字段的示例: ``` # filebeat.yml filebeat.inputs: - type: log paths: - /var/log/nginx/access.log processors: - dissect: tokenizer: "%{[@metadata][nginx][access]}" field: "message" target_prefix: "nginx.access" - grok: match: message: '%{IPORHOST:[nginx][access][remote_ip]} - %{DATA:[nginx][access][user_name]} \[%{HTTPDATE:[nginx][access][time]}\] "%{WORD:[nginx][access][http_method]} %{DATA:[nginx][access][http_version]}" %{NUMBER:[nginx][access][http_response_code]} %{NUMBER:[nginx][access][body_sent][bytes]} "%{DATA:[nginx][access][referrer]}" "%{DATA:[nginx][access][agent]}"' - date: match_formats: ["dd/MMM/YYYY:H:m:s Z"] target_field: "@timestamp" source_field: "[nginx][access][time]" fields_under_root: true fields: type: nginx output.elasticsearch: hosts: ["localhost:9200"] ``` 以上配置文件中使用了grok处理器,其中的正则表达式可以从Nginx日志中提取remote_ip、user_name、time、http_method、http_version、http_response_code、body_sent、referrer和agent等字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值