cutiechi
V2EX  ›  问与答

请教一个关于正则表达式的问题

  •  
  •   cutiechi · Apr 22, 2018 · 2412 views
    This topic created in 3149 days ago, the information mentioned may be changed or developed.

    给定一字符串:abc_de_fghi__pzf___56_

    请问如何使用正则匹配:abc de fghi pzf 56 ?

    8 replies  •  2018-04-24 02:54:50 +08:00
    Kaciras
    Kaciras
    Apr 22, 2018
    1
    [0-9a-z]+
    zgx030030
    zgx030030
    Apr 22, 2018 via iPhone
    2
    [a-zA-Z0-9]+
    还是我想简单了?😄
    ThirdFlame
    ThirdFlame
    Apr 22, 2018❤️1
    3
    给这个 abc_de_fghi__pzf___56_ 输出这个 abc de fghi pzf 56 ????
    难道不是 replce ?还要用 re ?
    cutiechi
    cutiechi
    OP
    Apr 22, 2018
    4
    @Kaciras
    @zgx030030
    @ThirdFlame

    不好意思是我没有表述清楚,我本意是给定一个字符串:_abc_de_fghi__pzf___56_ (_第一个_第二个_第三__第四个___第五个_) ,如何取得这五个字串?
    ifaii
    ifaii
    Apr 22, 2018
    5
    r = '_abc_de_fghi__pzf___56_'
    [ x for x in r.split('_') if x != '' ]

    L3 的方法更快了,要啥正则
    wevsty
    wevsty
    Apr 22, 2018
    6
    正则的话这样写就行了 [a-zA-Z0-9]+[^_]+
    不过说实话针对这种字符串,直接 split 更快。
    Arnie97
    Arnie97
    Apr 24, 2018 via Android
    7
    s.replace('_', '').split()
    Arnie97
    Arnie97
    Apr 24, 2018 via Android
    8
    楼上笔误
    s.replace('_', ' ').split()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2070 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:25 · PVG 21:25 · LAX 05:25 · JFK 08:25
    ♥ Do have faith in what you're doing.