2011-09-20 12 views
1

我對Python和正則表達式都很新,所以請耐心等待。我有一些文字,看起來像這樣:一些Python中的正則表達式的幫助

 
Change 421387 on 2011/09/20 by [email protected] 

    Some random text including line breaks 

Change 421388 on 2011/09/20 by [email protected] 

    Some other random text including line breaks 

現在,我要使用Python用正則表達式沿着這個分裂成一個元組。最後,我希望元組包含兩個元素。

元素0:

 
Change 421387 on 2011/09/20 by [email protected] 

    Some random text including line breaks 

要素1:

 
Change 421388 on 2011/09/20 by [email protected] 

    Some other random text including line breaks 

我知道我可以使用正則表達式來識別由所形成的圖案:

  • 單詞 「更改」
  • a space
  • 一些數字
  • 一些文本
  • 在形式####/##/##
  • 一些文本
  • @
  • 一些文本
  • 換行符
日期

我知道它可能會被進一步分解,但我認爲認識這些東西對我而言已經足夠了。

一旦我想出了該模式的正則表達式,我該如何使用它將字符串拆分爲字符串元組?

+2

「裸與我」是脫衣服的邀請。 – agf

回答

4

隨着前瞻斷言。

>>> re.split(r'(?=\s+Change \d+ on \d{4})\s+', ''' Change 421387 on 2011/09/20 by [email protected] 
...  Some random text including line breaks 
...  Change 421388 on 2011/09/20 by [email protected] 
...  Some other random text including line breaks''') 
['', 'Change 421387 on 2011/09/20 by [email protected]\n Some random text including line breaks', 'Change 421388 on 2011/09/20 by [email protected]\n Some other random text including line breaks'] 
+0

非常感謝!它會讓我永遠把它弄清楚。 –