如何在python中使用正则表达式

时间:2012-07-13 07:11:26

标签: python regex

对不起,这似乎是一个重复的问题,但我真的需要帮助

所以我有一个文本文件,其格式为:

  

Thu Apr 28 20:51:37 +0000 2011 :: Melanie Caldwell :: judeyqwaller ::   香港:: P000352670 - 东芝Satellite 5205系列触摸屏:   东芝Satellite 5205系列触摸板 - P000352670COMPATIB ...   http://t.co/QU5jA6U5

我只需拔出:: Hong Kong::之后开始的那一行,即 P000352670...,等等。

如何使用正则表达式执行此操作?

3 个答案:

答案 0 :(得分:3)

试试这个:

res = ' :: '.join(row.split(' :: ')[4:])

答案 1 :(得分:1)

你不需要正则表达式,这很简单,你可以这样做:

x = string.split("::")[-1]

如果字符串是您的文本行

编辑您的新问题(假设您使用的是python 2.5 +):

string = "682698_62876_26861"
print string.rpartition('_')[0]

这将完全输出您需要的内容:

682698_62876

答案 2 :(得分:1)

>>> row = "Thu Apr 28 20:51:37 +0000 2011 :: Melanie Caldwell :: judeyqwaller :: Hong Kong :: P000352670 - Toshiba Satellite 5205 Series TouchPad: Toshiba Satellite 5205 Series TouchPad - P000352670COMPATIB... http://t.co/QU5jA6U5"
>>> row.rpartition('::')[2]
' P000352670 - Toshiba Satellite 5205 Series TouchPad: Toshiba Satellite 5205 Series TouchPad - P000352670COMPATIB... http://t.co/QU5jA6U5'