正则表达式太难了 ..怎么破

2019-03-21 19:21:16 +08:00
 notgood
找了半天都没找出问题
求 V 友帮忙看看指点一下 谢谢!

log 如下 :

Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with <HOST>: authentication error

正规表达式如下:
failregex = ^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s+failed to handshake with <HOST>: authentication error$

手动测试无法匹配到


# fail2ban-regex 'Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with <HOST>: authentication error' '^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s+failed to handshake with <HOST>: authentication error$'

Running tests
=============

Use failregex line : ^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s...
Use single line : Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-0...


Results
=======

Failregex: 0 total

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
| [1] (?:DAY )?MON Day 24hour:Minute:Second(?:\.Microseconds)?(?: Year)?
`-

Lines: 1 lines, 0 ignored, 0 matched, 1 missed
[processed in 0.00 sec]

|- Missed line(s):
| Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with <HOST>: authentication error
`-
6432 次点击
所在节点    程序员
40 条回复
shenxgan
2019-03-21 20:00:05 +08:00
>>> re_s = '^\w+\s+\d+ \d+:\d+:\d+\s+(.*)ERROR:\s+failed to handshake with <HOST>: authentication error$'
>>> re.findall(re_s, s)
['<hostname> ss-server[1382]: 2018-08-15 08:59:07 ']
>>>

你的正则没有问题吧,我用 python 没问题
notgood
2019-03-21 20:39:33 +08:00
@shenxgan 我刚又试了你的也不能匹配, 真奇怪, 你说 python 没问题那正则表达式应该是正确的呀???
fail2ban-regex 'Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with <HOST>: authentication error' '^\w+\s+\d+ \d+:\d+:\d+\s+(.*)ERROR:\s+failed to handshake with <HOST>: authentication error$'
7654
2019-03-21 20:48:55 +08:00
antileech
2019-03-21 21:10:18 +08:00
排除法,先把头尾的^$摘了,看看行不行,不行再摘

或者像这样,反过来,从简单的写起来:
\d+:\d+:\d+\s+(.*)ERROR:
Varobjs
2019-03-21 21:14:02 +08:00
搜个正则表达式在线测试网站?
notgood
2019-03-21 21:17:38 +08:00
@7654 谢谢! 看了好久也没发现哪里异常啊, 您发现有什么不对的吗? 谢谢
notgood
2019-03-21 21:28:00 +08:00
@Varobjs 已经试了好几个测试网站 , 没有用
@antileech 您说的我试了, 还是不行, 简化后也是没有匹配
Varobjs
2019-03-21 21:33:19 +08:00
@notgood 正则在不同的语言,不同的地方使用的模式有差异的,比如之前遇到 grep 命令正则不支持\d, 加个 -P 就支持了,建议看见你用的语言支持的是不是有区别
notgood
2019-03-21 21:56:17 +08:00
@antileech 试过了很多参数都不行 . 比如这个 fail2ban-regex 'Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with 95.179.169.185: authentication error' '^\s+ERROR:\s+failed to handshake with <HOST>: authentication error$'

@7654
antileech
2019-03-21 22:23:30 +08:00
@notgood 9# 这条不是从行开头开始匹配,得把^去掉,另外 ip 中的.看看有没有转义
0mza987
2019-03-21 22:58:22 +08:00
你 reg 里那个 ERROR 前面的小 s 是什么鬼,是\s 吧
0mza987
2019-03-21 23:01:23 +08:00
要匹配什么东西,在达到目的的情况下 reg 尽量精简,你那头尾一大堆根本就不需要的匹配,只是变成扰乱自己实现的无效代码而已
herozzm
2019-03-22 00:39:18 +08:00
我手头有份爬虫匹配兼职 专门写正则的 lz 考虑拿来练手?
araraloren
2019-03-22 08:34:41 +08:00
不同语言的正则是有比较大的差别的,或许你可以了解一下 https://www.regular-expressions.info
JerryV2
2019-03-22 09:10:40 +08:00
__prefix_line 是什么,搜了半天也没看懂
wizardoz
2019-03-22 09:12:09 +08:00
正则表达式在不同的库里面支持规范是不一样的,就像不同的数据库支持的 SQL 也略有不同。
在 python 中测试了能匹配,只能说在 python 正则库中没问题
JerryV2
2019-03-22 09:17:14 +08:00
@JerryV2
仔细看了一下,是 Fail2ban 里的一个参数,是要匹配 <hostname> ss-server[1382]: 2018-08-15 08:59:07 这段吧?
没搞过,如果这块也没问题就不懂了
richieboy
2019-03-22 09:34:53 +08:00
这明显无法匹配啊,你括号不用转义吗?还是我理解的有问题?
versionzhang
2019-03-22 09:43:42 +08:00
@herozzm 功能复杂么,加个微信聊一下? base64 wx:U2VyZW5hZGVMaWZl
ghostsimon
2019-03-22 10:07:35 +08:00
^\w+\s+\d+ \d+:\d+:\d+\s+(.+)\s+ERROR:\s+failed to handshake with <HOST>: authentication error$
https://regex101.com/
可以测试通过

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/547142

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX