删除子字符串直到行尾

时间:2015-10-17 08:18:55

标签: python regex string

我有一个字符串uint16_t 14bitarr_get(unsigned char* arr, unsigned int index) { EightValues* accessPointer = (EightValues*)arr; accessPointer += index >> 3; //select the right structure in the array switch(index & 7) { //use the last three bits of the index to access the right bitfield case 0: return accessPointer->v0; case 1: return accessPointer->v1; case 2: return accessPointer->v2; case 3: return accessPointer->v3; case 4: return accessPointer->v4; case 5: return accessPointer->v5; case 6: return accessPointer->v6; case 7: return accessPointer->v7; } }

a

我想基本上做以下几点 a="to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"

以便输出如下所示:

a=a.replace_all("to be removed...\r\n" ,"")

然而,我只知道线是如何开始的,而不是其余的

3 个答案:

答案 0 :(得分:3)

使用re.sub并添加一个重复.的通配符*,但不要贪图? \r\n

>>> import re
>>> a = "to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"
>>> a = re.sub(r'to be removed.*?\r\n', '', a)
>>> a
'row 2 blah blah\r\nrow 3 more blah blah \r\nrow 5 final blah blah'

答案 1 :(得分:1)

不使用正则表达式的另一种方法:

Y

答案 2 :(得分:0)

另一种尝试可能是search模块的subre功能的组合 -

>>>  a = "to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"
>>>  r = re.sub(re.search(r'to be removed blah blah[\s]+', a,re.I).group(), '', a)
>>>  print r