Python 正则表达式合并的问题 - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Dragonish3600

Python 正则表达式合并的问题

  •  
  •   Dragonish3600 Jul 9, 2019 3490 views
    This topic created in 2499 days ago, the information mentioned may be changed or developed.

    pattern = re.compile(r'port (\d+)') pattern1 = re.compile(r'Logon ID:\t\t(\w+)\r\n\tLogon')

    怎么写才能把这 2 个合并到一起然后返回 ()即 \d+ \w+中的匹配呢? 分开写匹配正常

    试着如果写成

    pattern = re.compile(r'port (\d+)|Logon ID:\t\t(\w+)\r\n\tLogon') 

    这样会匹配不到返回 none

    而如果写成

    pattern = re.compile(r'( port (\d+))|( Logon ID:\t\t(\w+)\r\n\tLogon )') 

    这样返回的又是 整个括号中的字段,port (\d+)或者 Logon ID:\t\t(\w+)\r\n\tLogon )

    Supplement 1    Jul 9, 2019
    主要是想用一个来匹配 windows 和 linux 的安全日志,也就是在

    “”“
    2019-07-09 14:39:36,592 - /Volumes/unix/workplace/Inprogress/AMSReplaceBastions_test.py[line:93] - INFO: [Security] [4634] [Microsoft-Windows-Security-Auditing] [WIN] [An account was logged off.

    Subject:
    Security ID: S-1-5-21-2699825732-1888650521-3855454274-2782
    Account Name: administartor
    Account Domain: test
    Logon ID: 0x6800CD4

    Logon Type: 10
    ”“”


    """
    Starting session: shell on pts/0 for root from 10.0.0.70 port 45912 id 0
    """

    提取 windows 唯一的 loggin id 和 linux 唯一的 port
    10 replies    2019-07-17 08:24:45 +08:00
    bomb77
        1
    bomb77  
       Jul 9, 2019
    In [3]: re.compile(r'port(\d+)|Logon ID(\d+)')
    Out[3]: re.compile(r'port(\d+)|Logon ID(\d+)')

    In [4]: p1 = re.compile(r'port(\d+)|Logon ID(\d+)')

    In [5]: p1.match('Logon ID8Logon')
    Out[5]: <_sre.SRE_Match at 0x1099fee00>

    In [6]: m = p1.match('Logon ID8Logon')

    In [7]: m.groups()
    Out[7]: (None, '8')

    In [8]: m
    Out[8]: <_sre.SRE_Match at 0x109e43030>

    In [9]: m2 = p1.match('port5')

    In [10]: m2
    Out[10]: <_sre.SRE_Match at 0x109e430b8>

    In [11]: m2.groups()
    Out[11]: ('5', None)
    bomb77
        2
    bomb77  
       Jul 9, 2019
    @bomb77 #1 应该都能匹配出来的,只是匹配到的结果在 groups 的不同 index 里吧
    ipwx
        3
    ipwx  
       Jul 9, 2019
    用 ?: ?P 和 groupdict()

    http://ideone.com/qFhwVd
    Dragonish3600
        4
    Dragonish3600  
    OP
       Jul 9, 2019
    @ipwx 多谢
    请问如果不想返回 dict,只想获取 id,能做到么
    ipwx
        5
    ipwx  
       Jul 9, 2019
    @ladypxy 你都拿到 dict 了想怎么办就怎么办呗。

    if xxx.groupdict()['id'] is not None:
    ...
    elif xxx.groupdict()['port'] is not None:
    ...
    Dragonish3600
        6
    Dragonish3600  
    OP
       Jul 9, 2019 via iPhone
    @ipwx 代码字节数有限制……所以想尽量一次搞定啊
    ipwx
        7
    ipwx  
       Jul 9, 2019
    ipwx
        8
    ipwx  
       Jul 9, 2019
    2gua
        9
    2gua  
       Jul 11, 2019
    from typing import Optional, Match, Pattern, Tuple
    import re


    def matchs(target: str) -> Optional[str]:
    pat: Pattern[str] = re.compile(r"port\s*(\d+)|Logon ID:\s*(\w+)")
    res: Optional[Match[str]] = re.search(pat, target)
    if res:
    g: Tuple[Optional[str], Optional[str]] = res.groups()
    return g[0] if g[0] else g[1]
    return None
    Dragonish3600
        10
    Dragonish3600  
    OP
       Jul 17, 2019
    @livid, 帮忙删除下这个帖子,没注意泄漏了不少信息。。
    About     Help     Advertise     Blog     API     FAQ     Solana     3279 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 196ms UTC 14:12 PVG 22:12 LAX 07:12 JFK 10:12
    Do have faith in what you're doing.
    ubao msn 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