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
AzsharR
V2EX  ›  Python

大佬们怎么用正则定位到 这样的内容 XXXXXX : 8

  •  
  •   AzsharR · 2020-09-23 11:16:46 +08:00 · 1789 次点击
    这是一个创建于 1282 天前的主题,其中的信息可能已经有所发展或是发生改变。
    processor : 0
    BogoMIPS : 38.40
    Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
    CPU implementer : 0x51
    CPU architecture: 8
    CPU variant : 0x2
    CPU part : 0x201
    CPU revision : 1

    processor : 1
    BogoMIPS : 38.40
    Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
    CPU implementer : 0x51
    CPU architecture: 8
    CPU variant : 0x2
    CPU part : 0x201
    CPU revision : 1


    就是定位到上面这样的

    下面是我自己写的
    re.findall(r'processor[\s\w\d]{1}', a)
    结果是['processor\t', 'processor\t', 'processor\t', 'processor\t']
    并定位不到 完整的内容
    9 条回复    2020-09-29 10:50:40 +08:00
    forty
        1
    forty  
       2020-09-23 11:22:26 +08:00   ❤️ 2
    定啥定啊,直接按连续 2 个换行符分割不就行了吗?
    Rxianbei
        2
    Rxianbei  
       2020-09-23 11:23:29 +08:00 via Android
    @forty 很有道理
    umissthestars
        3
    umissthestars  
       2020-09-23 11:31:02 +08:00
    /(\n.*?architecture\s?[::]\s?8.*?)(?!\n)/gi


    // example
    ```
    const str = `processor : 0 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x201 CPU revision : 1

    processor : 1 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x201 CPU revision : 1

    processor : 2 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x205 CPU revision : 1

    processor : 3 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x205 CPU revision : 1`
    ```
    // output
    ```
    ["↵processor : 1 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8", "↵processor : 2 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8", "↵processor : 3 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8"]
    ```
    qazwsxkevin
        4
    qazwsxkevin  
       2020-09-23 11:31:04 +08:00
    import re
    tarString = "CPU architecture: 8"
    getStr = re.sub('(?<=:).*?(?=$")', 'utf-8', tarString)

    # 方法 2,必定是数字的话
    getStr = re.findall(r"\d+",tarString)
    AzsharR
        5
    AzsharR  
    OP
       2020-09-23 11:32:46 +08:00
    @forty 因为每次,内容不同,所以需要定位 processor 这个关键词查看后面内容,但是又因为 processor 后面又很多空格定位不到
    umissthestars
        6
    umissthestars  
       2020-09-23 11:33:53 +08:00
    你这输入和我进来时的不一样了...你又偷偷更新了
    AzsharR
        7
    AzsharR  
    OP
       2020-09-23 11:36:06 +08:00
    @umissthestars 别不好意思 因为呈现出来的格式打乱了 改了一下
    caaaalabash
        8
    caaaalabash  
       2020-09-23 16:39:31 +08:00
    /(processor.+?)(?=processor)|(processor.+)(?=$)/sg
    wd941113
        9
    wd941113  
       2020-09-29 10:50:40 +08:00
    string = "processor:****"
    base_string_split = [{i.split(":")[0].strip(): i.split(":")[1].strip() for i in temp.strip("\n").split("\n")} for temp in string.split("\n\n")]
    for string_temp in base_string_split:
    processor = string_temp.get(processor)

    --over--
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1538 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:16 · PVG 01:16 · LAX 10:16 · JFK 13:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.