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

正则提取又蒙圈了!

  •  
  •   maloneleo88 · 2021-03-09 22:50:13 +08:00 · 2340 次点击
    这是一个创建于 1134 天前的主题,其中的信息可能已经有所发展或是发生改变。
    [18,3,5,7,5,2,3,3,14,14],[19,3,7,3,8,5,0,1,15,12],[20,3,7,3,9,1,1,2,19,7],[24,4,4,8,4,5,1,2,9,19],[25,2,5,7,4,6,1,3,13,15],[26,1,6,10,2,4,4,1,13,15],[27,6,4,2,3,5,4,4,13,15],[28,2,1,12,7,2,2,1,11,16],[29,5,6,9,5,1,1,1,13,15],[30,2,6,8,4,1,2,4,15,12],[31,0,7,8,5,3,1,3,14,13],[33,1,6,8,5,3,1,3,14,13],[35,3,3,8,6,3,3,2,13,15],[46,4,9,7,3,2,2,1,14,14],[51,0,11,6,8,1,2,0,21,7],[52,3,7,6,6,3,2,1,15,13],[56,1,7,3,6,2,3,5,19,8],[59,1,3,8,9,4,1,2,14,14],[60,4,6,5,7,2,1,2,14,13],[62,2,4,4,10,5,1,1,15,12]


    怎么提取其中一个列表啊? re.findall(r'\[18,(\d+)\],',list) 为什么提取不到呢? 只能提取全部数字。 比如提取第一个列表,以 18 这个元素定位应该怎么写哇?
    11 条回复    2021-03-10 02:39:50 +08:00
    VTEX9527
        1
    VTEX9527  
       2021-03-09 22:55:51 +08:00
    r'\[18(,\d+){9}\],'
    maloneleo88
        2
    maloneleo88  
    OP
       2021-03-09 23:01:56 +08:00
    @VTEX9527 不行呀 逗号扔里面也不行
    maloneleo88
        3
    maloneleo88  
    OP
       2021-03-09 23:07:51 +08:00
    @VTEX9527 得到 [',14']
    jeffwcx
        4
    jeffwcx  
       2021-03-09 23:23:47 +08:00
    /\[18(,?\d+){9}\]/ 这样不就好了吗
    ETiV
        5
    ETiV  
       2021-03-09 23:32:38 +08:00
    \[18,[,0-9]+\]

    虽然……不知道你到底想干啥……
    jeffwcx
        6
    jeffwcx  
       2021-03-09 23:41:05 +08:00
    /\[18(,\d+){9}\]/g 这个加 global 也是可以的
    VTEX9527
        7
    VTEX9527  
       2021-03-09 23:42:09 +08:00
    @maloneleo88
    楼上正则都是没有问题的,主要应该是 re 模块的实现不一样。python 中 re.findall()是匹配分组的,您要字符串中第一个数组。我暂时能想到的是换一个写法。
    str_regex = r'\[18(,\d+){9}\]'
    list_value = re.finditer(str_regex, input, flags=re.I)
    for match in list_value:
    first_array_str = match.group()
    print(first_array_str)

    然后,自行处理 first_array_str
    webshe11
        8
    webshe11  
       2021-03-09 23:43:21 +08:00
    不知道楼主想干啥,有种 X-Y Problem 的气息
    如果不用非用正则表达式,左右补上 '[' ']' 然后 json.loads() 比较好处理
    Kasumi20
        9
    Kasumi20  
       2021-03-09 23:46:13 +08:00
    /\[18.*?\]/
    maloneleo88
        10
    maloneleo88  
    OP
       2021-03-10 00:14:03 +08:00
    @VTEX9527
    @jeffwcx
    @ETiV
    @jeffwcx
    @VTEX9527
    @webshe11
    @Kasumi20

    谢谢大家,搞了半天,新人,只会正则和 xpath, 经 @webshe11 提醒,json.loads 试了一下,NB 多了 。 哈哈。没办法懂的少,下次需求描述清楚些。
    @VTEX9527 老哥辛苦了!你说的对。问题不在正则,是我有锤子不用,拿斧子劈砖
    ETiV
        11
    ETiV  
       2021-03-10 02:39:50 +08:00 via iPhone
    手动捂脸🤦‍♂️
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1038 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:38 · PVG 03:38 · LAX 12:38 · JFK 15:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.