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

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
`-
6448 次点击
所在节点    程序员
40 条回复
ghostsimon
2019-03-22 10:10:31 +08:00
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^\w+\s+\d+ \d+:\d+:\d+\s+(.+)\s+ERROR:\s+failed to handshake with <HOST>: authentication error$"

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

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1

print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
jinhan13789991
2019-03-22 10:20:06 +08:00
有这么复杂吗

.*ERROR.*<HOST>.*

还是说我理解的不对?
zhijiansha
2019-03-22 12:38:45 +08:00
@herozzm 有意练手,wx empzMzk0NQ==
Valid
2019-03-22 13:59:41 +08:00
Valid
2019-03-22 14:00:41 +08:00
l00t
2019-03-22 15:16:22 +08:00
不就是错在 prefix_line 后面的那个 s 上吗
notgood
2019-03-22 15:18:48 +08:00
@Valid 请问您用的调试网站是哪个? 为什么我测试同样的表达式还是不匹配 ? 在 fail2ban 也不匹配
@ghostsimon 我测试了没通过 , 在你的那个网站,
atonku
2019-03-22 15:29:53 +08:00
正则表达式本来就不是让人看的,淡定
xuboying
2019-03-22 15:37:43 +08:00
正则表达式如果错了就简化一下再一点一点加,比如设一个最点单的 一个字符的 . 理论上百分之一百匹配上
有些奇怪的错误是因为程序的正则引擎和你实验的不同。比如老版本的 gcc 用了一个假的 stl 正则库还像模像样的执行了一下。。。。
showHand043
2019-03-22 16:50:05 +08:00
遇见正则都是百度谷歌
marsgt
2019-03-22 17:01:21 +08:00
推荐个网站吧:
https://regex101.com/
ghostsimon
2019-03-22 17:03:16 +08:00
@notgood
可能你写的正则表达式不对吧,没看懂你正则里面的%(__prefix_line)s 是什么意思,分组的话,(.+)就可以了。
fox0001
2019-03-22 19:54:21 +08:00
推荐一本书《正则表达式必知必会》,简单易懂。

当年我好奇与正则表达式,啃熟了,非常实用,尤其是各种文本查找替换的场合
napoleongp
2019-03-22 22:40:54 +08:00
fail2ban ?前面是时间的正则吗?那段删掉试试
napoleongp
2019-03-22 22:51:26 +08:00
zoffy
2019-03-22 23:37:41 +08:00
cpdyj0
2019-03-22 23:39:39 +08:00
楼上 regex101.com +1 可以在线 DEBUG,能看执行步骤,优化性能
Kylin30
2019-03-22 23:45:12 +08:00
搞正则的必须要十万一个月
doraos
2019-03-23 10:53:15 +08:00
不需要死记硬背, 用到就查,或者看看书<楼上那本>系统的学一下
Valid
2019-03-26 12:04:45 +08:00

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

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

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

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

© 2021 V2EX