【发布时间】:2013-06-12 03:57:29
【问题描述】:
我有一个复杂的字符串,想尝试从中提取多个子字符串。
字符串由一组项目组成,以逗号分隔。每个项目都有一个标识符(id-n),用于在括号中括起来的一对单词。我只想得到括号内的单词,它的末尾附有一个数字(例如'This-1')。数字实际上表示提取后单词应该如何排列的位置。
#Example of how the individual items would look like
id1(attr1, is-2) #The number 2 here indicates word 'is' should be in position 2
id2(attr2, This-1) #The number 1 here indicates word 'This' should be in position 1
id3(attr3, an-3) #The number 3 here indicates word 'an' should be in position 3
id4(attr4, example-4) #The number 4 here indicates word 'example' should be in position 4
id5(attr5, example-4) #This is a duplicate of the word 'example'
#Example of string - this is how the string with the items looks like
string = "id1(attr1, is-1), id2(attr2, This-2), id3(attr3, an-3), id4(attr4, example-4), id5(atttr5, example-4)"
#This is how the result should look after extraction
result = 'This is an example'
有没有更简单的方法来做到这一点?正则表达式对我不起作用。
【问题讨论】:
-
我无法理解您的示例。你能尝试用不同的方式来描述它吗?
-
@DaoWen - 抱歉,字符串本身有点复杂。很难形容。
-
什么控制着项目的重新排序?
-
@IgnacioVazquez-Abrams - 括号中第二个单词末尾的数字。
-
另一个例子发生了什么?