【问题标题】:Non-consuming regular expression split in PythonPython中的非消耗正则表达式拆分
【发布时间】:2011-04-27 09:27:13
【问题描述】:

如何在分隔符表达式上拆分字符串,同时将该分隔符保留在前面的字符串上?

>>> text = "This is an example. Is it made up of more than once sentence? Yes, it is."
>>> re.split("[\.\?!] ", text)
['This is an example', 'Is it made up of more than one sentence', 'Yes, it is.']

我希望结果是。

['This is an example.', 'Is it made up of more than one sentence?', 'Yes, it is.']

到目前为止,我只尝试了一个前瞻断言,但这根本无法拆分。

【问题讨论】:

    标签: python regex


    【解决方案1】:
    >>> re.split("(?<=[\.\?!]) ", text)
    ['This is an example.', 'Is it made up of more than once sentence?', 'Yes, it is.']
    

    关键是在?&lt;= 中使用look-behind 断言。

    【讨论】:

    • +1,经过仔细考虑 ;-)。我也认为简洁的答案会更好,因为它突出了重要的事情。
    • @all @ThomasH 到目前为止,我提请十位赞成者的注意,他们赞成一个错误的答案:此答案中使用的分隔符是空白,并且此空白不是保留在每个分隔字符串,与 OP 的关键要求相反。 ThomasH,我提请您注意这样一个事实,即即使答案很简洁,如果它是错误的答案也不会是更好的答案。
    【解决方案2】:
    import re
    
    text = "This is an example.A particular case.Made up of more "\
           "than once sentence?Yes, it is.But no blank !!!That's"\
           " a problem ????Yes.I think so! :)"
    
    
    for x in re.split("(?<=[\.\?!]) ", text):
        print repr(x)
    
    print '\n'
    
    for x in re.findall("[^.?!]*[.?!]|[^.?!]+(?=\Z)",text):
        print repr(x)
    

    结果

    "This is an example.A particular case.Made up of more than once sentence?Yes, it is.But no blank !!!That'sa problem ????Yes.I think so!"
    ':)'
    
    
    'This is an example.'
    'A particular case.'
    'Made up of more than once sentence?'
    'Yes, it is.'
    'But no blank !'
    '!'
    '!'
    "That's a problem ?"
    '?'
    '?'
    '?'
    'Yes.'
    'I think so!'
    ' :)'
    

    .

    编辑

    还有

    import re
    
    text = "! This is an example.A particular case.Made up of more "\
           "than once sentence?Yes, it is.But no blank !!!That's"\
           " a problem ????Yes.I think so! :)"
    
    res = re.split('([.?!])',text)
    
    print [ ''.join(res[i:i+2]) for i in xrange(0,len(res),2) ]
    

    给予

    ['!', ' This is an example.', 'A particular case.', 'Made up of more than once sentence?', 'Yes, it is.', 'But no blank !', '!', '!', "That's a problem ?", '?', '?', '?', 'Yes.', 'I think so!', ' :)']
    

    【讨论】:

    • @someone 感谢那些支持我的回答的人,在所有情况下都给出了正确的结果(我希望),而其他一些易受影响的马修效应(我想这是原因)人继续支持仅在有限情况下给出正确结果的其他答案。
    • @eyquem Wauw,这并不是真正引起同情的方式。简单的答案给出了有限情况的正确结果 which 与 OP 要求的完全匹配。很容易引入进一步的复杂性(在场景和解决方案中),特别是对于与正则表达式相关的问题,但增加的复杂性并不总是用于任何目的(或在现实世界的需求中占有一席之地)。换句话说:您在其他答案中发现的缺点可能是有效的,但实际上只能由 OP 来决定。
    • @jensgram 我认为一个好的答案不能仅仅回答问题中考虑的分隔情况。这是对良好代码的热爱和对提出问题的人的尊重,这可能不会考虑到解决方案实际上会遇到的所有特殊但并非罕见的情况。
    • @jensgram 我不同意你的表述“很容易引入进一步的复杂性” 1)因为这不是引入的问题,而是现实的问题: “90% 的编码是调试。另外 10% 是编写错误。Bram Cohen。” 所以一个有趣的答案是通过给出提问者没有提出的想法来拓宽问题的上下文不要思考,这会警告她/他注意潜在的错误。
    • @jensgram 我不会假装我总是那样做:很多时候,问题中缺乏足够的信息会阻碍做出一个成熟的答案,因为它会是一个过于密集的答案,考虑太多可能性;或者我没有足够的时间,或者我没有太多灵感,等等。但我并没有从认为简约的答案总是最好的想法开始。
    猜你喜欢
    • 2021-10-07
    • 2012-04-20
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多