![]() | 1 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 没问题 |
2 notgood OP @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$' |
![]() | 3 7654 2019-03-21 20:48:55 +08:00 |
![]() | 4 antileech 2019-03-21 21:10:18 +08:00 排除法,先把头尾的^$摘了,看看行不行,不行再摘 或者像这样,反过来,从简单的写起来: \d+:\d+:\d+\s+(.*)ERROR: |
![]() | 5 Varobjs &nsp; 2019-03-21 21:14:02 +08:00 via Android 搜个正则表达式在线测试网站? |
7 notgood OP |
![]() | 8 Varobjs 2019-03-21 21:33:19 +08:00 via Android @notgood 正则在不同的语言,不同的地方使用的模式有差异的,比如之前遇到 grep 命令正则不支持\d, 加个 -P 就支持了,建议看见你用的语言支持的是不是有区别 |
9 notgood OP |
11 0mza987 2019-03-21 22:58:22 +08:00 你 reg 里那个 ERROR 前面的小 s 是什么鬼,是\s 吧 |
12 0mza987 2019-03-21 23:01:23 +08:00 要匹配什么东西,在达到目的的情况下 reg 尽量精简,你那头尾一大堆根本就不需要的匹配,只是变成扰乱自己实现的无效代码而已 |
![]() | 13 herozzm 2019-03-22 00:39:18 +08:00 via iPhone 我手头有份爬虫匹配兼职 专门写正则的 lz 考虑拿来练手? |
![]() | 14 araraloren 2019-03-22 08:34:41 +08:00 不同语言的正则是有比较大的差别的,或许你可以了解一下 https://www.regular-expressions.info |
15 JerryV2 2019-03-22 09:10:40 +08:00 __prefix_line 是什么,搜了半天也没看懂 |
![]() | 16 wizardoz 2019-03-22 09:12:09 +08:00 正则表达式在不同的库里面支持规范是不一样的,就像不同的数据库支持的 SQL 也略有不同。 在 python 中测试了能匹配,只能说在 python 正则库中没问题 |
17 JerryV2 2019-03-22 09:17:14 +08:00 @JerryV2 仔细看了一下,是 Fail2ban 里的一个参数,是要匹配 <hostname> ss-server[1382]: 2018-08-15 08:59:07 这段吧? 没搞过,如果这块也没问题就不懂了 |
![]() | 18 richieboy 2019-03-22 09:34:53 +08:00 这明显无法匹配啊,你括号不用转义吗?还是我理解的有问题? |
19 versionzhang 2019-03-22 09:43:42 +08:00 via Android @herozzm 功能复杂么,加个微信聊一下? base64 wx:U2VyZW5hZGVMaWZl |
20 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/ 可以测试通过 |
21 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. |
![]() | 22 jinhan13789991 2019-03-22 10:20:06 +08:00 有这么复杂吗 .*ERROR.*<HOST>.* 还是说我理解的不对? |
![]() | 24 Valid 2019-03-22 13:59:41 +08:00  |
![]() | 25 Valid 2019-03-22 14:00:41 +08:00 |
26 l00t 2019-03-22 15:16:22 +08:00 不就是错在 prefix_line 后面的那个 s 上吗 |
27 notgood OP |
![]() | 28 atonku 2019-03-22 15:29:53 +08:00 正则表达式本来就不是让人看的,淡定 |
![]() | 29 xuboying 2019-03-22 15:37:43 +08:00 正则表达式如果错了就简化一下再一点一点加,比如设一个最点单的 一个字符的 . 理论上百分之一百匹配上 有些奇怪的错误是因为程序的正则引擎和你实验的不同。比如老版本的 gcc 用了一个假的 stl 正则库还像模像样的执行了一下。。。。 |
![]() | 30 showHand043 2019-03-22 16:50:05 +08:00 遇见正则都是百度谷歌 |
![]() | 31 marsgt 2019-03-22 17:01:21 +08:00 推荐个网站吧: https://regex101.com/ |
32 ghostsimon 2019-03-22 17:03:16 +08:00 @notgood 可能你写的正则表达式不对吧,没看懂你正则里面的%(__prefix_line)s 是什么意思,分组的话,(.+)就可以了。 |
33 fox0001 2019-03-22 19:54:21 +08:00 via Android 推荐一本书《正则表达式必知必会》,简单易懂。 当年我好奇与正则表达式,啃熟了,非常实用,尤其是各种文本查找替换的场合 |
34 napoleongp 2019-03-22 22:40:54 +08:00 fail2ban ?前面是时间的正则吗?那段删掉试试 |
35 napoleongp 2019-03-22 22:51:26 +08:00 |
![]() | 36 zoffy 2019-03-22 23:37:41 +08:00 |
![]() | 37 cpdyj0 2019-03-22 23:39:39 +08:00 楼上 regex101.com +1 可以在线 DEBUG,能看执行步骤,优化性能 |
![]() | 38 Kylin30 2019-03-22 23:45:12 +08:00 搞正则的必须要十万一个月 |
![]() | 39 doraos 2019-03-23 10:53:15 +08:00 不需要死记硬背, 用到就查,或者看看书<楼上那本>系统的学一下 |