大佬们怎么用正则定位到 这样的内容 XXXXXX : 8 - V2EX
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 2589 次点击
    这是一个创建于 1917 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     5388 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 41ms UTC 09:13 PVG 17:13 LAX 01:13 JFK 04:13
    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