POSIX正则表达式不适用于[:digit:]字符类 [英] POSIX regex not working with [:digit:] character class

查看:111
本文介绍了POSIX正则表达式不适用于[:digit:]字符类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在日志文件access_20170118_14.log-

127.0.0.1 - - [18/Jan/2017:14:22:16 +0000]  "GET //fam/shared_generate_test_devicelist.php HTTP/1.1" 200 168 "-" "curl/7.40.0" - 0.008
127.0.0.1 - - [18/Jan/2017:14:22:24 +0000]  "GET //fam/shared_generate_test_devicelist.php HTTP/1.0" 200 168 "-" "curl/7.40.0" - 0.008

以下是nginx logwarn命令,该命令从该日志文件中打印出HTTP访问代码不同于200和204的那些日志行.

Following is an nginx logwarn command which prints out those log lines from this log file which have an HTTP access code different from 200 and 204.

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx_access /mnt/log/nginx/access_20170118_14.log '!.*HTTP/1.(1|0)\"\s*(200|204)' 

上面命令中的正则表达式可以正常工作.

The regex in the above command works as expected.

但是,我要删除(1|0),将其添加到与HTTP版本1.0和1.1匹配的位置,并用任何数字替换它,以便它可用于任何版本的HTTP.

However, I want to get rid of the (1|0), which are added to match the HTTP versions 1.0 and 1.1, and replace it with any digit, so that it works for any version of HTTP.

以下带有[:digit:]字符类的命令与HTTP版本1.1和1.0以及访问代码200-

The following command, with the [:digit:] character class however matches the lines with HTTP version 1.1 and 1.0 and with access code 200 -

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx_access /mnt/log/nginx/access_20170118_14.log '!.*HTTP/1.[:digit:]\"\s*(200|204)' 

输出(不是我想要的)-

Output (not what I desire) -

Log errors: 127.0.0.1 - - [18/Jan/2017:14:22:16 +0000]  "GET //fam/shared_generate_test_devicelist.php HTTP/1.1" 200 168 "-" "curl/7.40.0" - 0.008
127.0.0.1 - - [18/Jan/2017:14:22:24 +0000]  "GET //fam/shared_generate_test_devicelist.php HTTP/1.0" 200 168 "-" "curl/7.40.0" - 0.008

推荐答案

POSIX字符类应在正则表达式字符类中使用,因此单个数字可以表示为:

POSIX character classes should be used inside a regular expression character class, so a single digit could be represented as:

[[:digit:]]

请注意双括号.有关更多信息,请参见本文档.

Notice the double brackets. See this document for more.

此外,您有一个裸露的.代表一个句点(我想)-应该用反斜杠转义为\.来代表一个文字句点.

Also, you have a naked . representing a period (I presume) - which should be escaped with a backslash as \. to represent a literal period.

这篇关于POSIX正则表达式不适用于[:digit:]字符类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆