V2EX = way to explore
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
ladypxy
V2EX  ›  Python

Python 正则表达式合并的问题

  •  
  •   ladypxy · 2019-07-09 09:50:13 +08:00 · 2600 次点击
    这是一个创建于 1743 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 )

    第 1 条附言  ·  2019-07-09 12:46:47 +08:00
    主要是想用一个来匹配 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 条回复    2019-07-17 08:24:45 +08:00
    bomb77
        1
    bomb77  
       2019-07-09 10:19:01 +08:00
    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  
       2019-07-09 10:21:48 +08:00
    @bomb77 #1 应该都能匹配出来的,只是匹配到的结果在 groups 的不同 index 里吧
    ipwx
        3
    ipwx  
       2019-07-09 10:34:34 +08:00
    用 ?: ?P 和 groupdict()

    http://ideone.com/qFhwVd
    ladypxy
        4
    ladypxy  
    OP
       2019-07-09 12:36:37 +08:00
    @ipwx 多谢
    请问如果不想返回 dict,只想获取 id,能做到么
    ipwx
        5
    ipwx  
       2019-07-09 16:25:13 +08:00
    @ladypxy 你都拿到 dict 了想怎么办就怎么办呗。

    if xxx.groupdict()['id'] is not None:
    ...
    elif xxx.groupdict()['port'] is not None:
    ...
    ladypxy
        6
    ladypxy  
    OP
       2019-07-09 16:31:06 +08:00 via iPhone
    @ipwx 代码字节数有限制……所以想尽量一次搞定啊
    ipwx
        7
    ipwx  
       2019-07-09 23:24:54 +08:00
    ipwx
        8
    ipwx  
       2019-07-09 23:27:10 +08:00
    2gua
        9
    2gua  
       2019-07-11 12:07:53 +08:00
    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
    ladypxy
        10
    ladypxy  
    OP
       2019-07-17 08:24:45 +08:00
    @livid, 帮忙删除下这个帖子,没注意泄漏了不少信息。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2017 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:16 · PVG 00:16 · LAX 09:16 · JFK 12:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.