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

求一个 Python 正则表达式

  •  
  •   ashes1122 · 2020-05-20 16:54:05 +08:00 · 2647 次点击
    这是一个创建于 1427 天前的主题,其中的信息可能已经有所发展或是发生改变。

    工作上需要写个 python 脚本,取出一串字符种第五个,前面的字符,迫于现学的 python,实在写不出来了,求 v 友帮写一下,谢谢 v 友们了。

    字符串如下:
    

    A1234, A1457, A1516, A1518, A1528, A1529, A1530, A1533, iPhone 5s, ME341LL/A, ME342LL/A, ME344LL/A, ME345LL/A, ME347LL/A, ME348LL/A, ME349LL/A, ME432LP/A

    15 条回复    2020-05-25 19:19:21 +08:00
    ranleng
        1
    ranleng  
       2020-05-20 16:58:14 +08:00   ❤️ 1
    直接 split 然后 [:5] 再 join 一下?
    jdhao
        2
    jdhao  
       2020-05-20 17:07:54 +08:00 via Android   ❤️ 1
    😂
    lonelinsky
        3
    lonelinsky  
       2020-05-20 17:09:02 +08:00   ❤️ 1
    import re
    groups = re.match('^(([^,]*,){5})', s).groups()
    result = groups[0][:-1] if groups else None
    cnmllll
        4
    cnmllll  
       2020-05-20 17:10:01 +08:00   ❤️ 1
    l = s.split(",")
    之后想怎么处理都行了
    ashes1122
        5
    ashes1122  
    OP
       2020-05-20 17:14:10 +08:00
    好的,谢谢各位。
    @ranleng
    @jdhao
    @lonelinsky
    @cnmllll
    HashV2
        6
    HashV2  
       2020-05-20 17:17:04 +08:00
    result = ",".join(s.split(",")[:5])
    为啥非要正则,有硬性要求吗?
    ashes1122
        7
    ashes1122  
    OP
       2020-05-20 17:23:45 +08:00
    @HashV2 现学,其他的更不知道咋搞。
    resist
        8
    resist  
       2020-05-20 17:28:33 +08:00
    你这个需求太简单了,建议直接操作字符串,如果是自己玩玩,别人用不着,那就可以使用正则
    HashV2
        9
    HashV2  
       2020-05-20 17:30:10 +08:00
    @ashes1122 可以 我是工作一年多了 《流畅的 python 》一定要看
    还有标准库 https://docs.python.org/zh-cn/3/library/index.html
    加油~~
    L00kback
        10
    L00kback  
       2020-05-20 17:45:57 +08:00
    @HashV2 老哥,你这是取了前 6 个内容,不对啊,应该是 result=s.split(",")[4].strip()
    L00kback
        11
    L00kback  
       2020-05-20 17:47:23 +08:00
    需求说的不太清楚,如果是开头到第五个逗号之间的字符串就是 result = ",".join(s.split(",")[:4])
    lonelinsky
        12
    lonelinsky  
       2020-05-20 18:04:04 +08:00
    @L00kback 你重新数下看看? Python 的 slice 是前闭后开的。


    这个问题还有一个点是如何处理 ",“ 数量小于 5 的情况。另外从性能的角度来考虑 split + join 的方法应该是优于 正则 的。
    Telegram
        13
    Telegram  
       2020-05-20 18:28:52 +08:00
    同意一楼,能 split 的尽量用这个
    ericls
        14
    ericls  
       2020-05-21 04:31:58 +08:00 via iPhone
    Regex the problem for all your solutions
    soraping
        15
    soraping  
       2020-05-25 19:19:21 +08:00
    reg_str = '.*?(\w{5}).*?'

    list_str = re.findall(reg_str, str11)

    print(list_str)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1997 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:20 · PVG 00:20 · LAX 09:20 · JFK 12:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.