正则表达式太难了 ..怎么破 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
notgood
V2EX    程序员

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

  •  
  •   notgood 2019-03-21 19:21:16 +08:00 7163 次点击
    这是一个创建于 2398 天前的主题,其中的信息可能已经有所发展或是发生改变。
    找了半天都没找出问题
    求 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
    `-
    40 条回复    2019-03-26 12:04:45 +08:00
    shenxgan
        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 没问题
    notgood
        2
    notgood  
    OP
       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$'
    antileech
        4
    antileech  
       2019-03-21 21:10:18 +08:00
    排除法,先把头尾的^$摘了,看看行不行,不行再摘

    或者像这样,反过来,从简单的写起来:
    \d+:\d+:\d+\s+(.*)ERROR:
    Varobjs
        5
    Varobjs  
    &nsp;  2019-03-21 21:14:02 +08:00 via Android
    搜个正则表达式在线测试网站?
    notgood
        6
    notgood  
    OP
       2019-03-21 21:17:38 +08:00
    @7654 谢谢! 看了好久也没发现哪里异常啊, 您发现有什么不对的吗? 谢谢
    notgood
        7
    notgood  
    OP
       2019-03-21 21:28:00 +08:00
    @Varobjs 已经试了好几个测试网站 , 没有用
    @antileech 您说的我试了, 还是不行, 简化后也是没有匹配
    Varobjs
        8
    Varobjs  
       2019-03-21 21:33:19 +08:00 via Android
    @notgood 正则在不同的语言,不同的地方使用的模式有差异的,比如之前遇到 grep 命令正则不支持\d, 加个 -P 就支持了,建议看见你用的语言支持的是不是有区别
    notgood
        9
    notgood  
    OP
       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
        10
    antileech  
       2019-03-21 22:23:30 +08:00
    @notgood 9# 这条不是从行开头开始匹配,得把^去掉,另外 ip 中的.看看有没有转义
    0mza987
        11
    0mza987  
       2019-03-21 22:58:22 +08:00
    你 reg 里那个 ERROR 前面的小 s 是什么鬼,是\s 吧
    0mza987
        12
    0mza987  
       2019-03-21 23:01:23 +08:00
    要匹配什么东西,在达到目的的情况下 reg 尽量精简,你那头尾一大堆根本就不需要的匹配,只是变成扰乱自己实现的无效代码而已
    herozzm
        13
    herozzm  
       2019-03-22 00:39:18 +08:00 via iPhone
    我手头有份爬虫匹配兼职 专门写正则的 lz 考虑拿来练手?
    araraloren
        14
    araraloren  
       2019-03-22 08:34:41 +08:00
    不同语言的正则是有比较大的差别的,或许你可以了解一下 https://www.regular-expressions.info
    JerryV2
        15
    JerryV2  
       2019-03-22 09:10:40 +08:00
    __prefix_line 是什么,搜了半天也没看懂
    wizardoz
        16
    wizardoz  
       2019-03-22 09:12:09 +08:00
    正则表达式在不同的库里面支持规范是不一样的,就像不同的数据库支持的 SQL 也略有不同。
    在 python 中测试了能匹配,只能说在 python 正则库中没问题
    JerryV2
        17
    JerryV2  
       2019-03-22 09:17:14 +08:00
    @JerryV2
    仔细看了一下,是 Fail2ban 里的一个参数,是要匹配 <hostname> ss-server[1382]: 2018-08-15 08:59:07 这段吧?
    没搞过,如果这块也没问题就不懂了
    richieboy
        18
    richieboy  
       2019-03-22 09:34:53 +08:00
    这明显无法匹配啊,你括号不用转义吗?还是我理解的有问题?
    versionzhang
        19
    versionzhang  
       2019-03-22 09:43:42 +08:00 via Android
    @herozzm 功能复杂么,加个微信聊一下? base64 wx:U2VyZW5hZGVMaWZl
    ghostsimon
        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/
    可以测试通过
    ghostsimon
        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.
    jinhan13789991
        22
    jinhan13789991  
       2019-03-22 10:20:06 +08:00
    有这么复杂吗

    .*ERROR.*<HOST>.*

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

    当年我好奇与正则表达式,啃熟了,非常实用,尤其是各种文本查找替换的场合
    napoleongp
        34
    napoleongp  
       2019-03-22 22:40:54 +08:00
    fail2ban ?前面是时间的正则吗?那段删掉试试
    napoleongp
        35
    napoleongp  
       2019-03-22 22:51:26 +08:00
    zoffy
        36
    zoffy  
       2019-03-22 23:37:41 +08:00
    cpdyj0
        37
    cpdyj0  
       2019-03-22 23:39:39 +08:00
    楼上 regex101.com +1 可以在线 DEBUG,能看执行步骤,优化性能
    Kylin30
        38
    Kylin30  
       2019-03-22 23:45:12 +08:00
    搞正则的必须要十万一个月
    doraos
        39
    doraos  
       2019-03-23 10:53:15 +08:00
    不需要死记硬背, 用到就查,或者看看书<楼上那本>系统的学一下
    Valid
        40
    Valid  
       2019-03-26 12:04:45 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1116 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 23:09 PVG 07:09 LAX 16:09 JFK 19:09
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86