Python 正则表达式在字符串中查找字符串而不考虑顺序?

Python 正则表达式在字符串中查找字符串而不考虑顺序?,python,regex,Python,Regex,我不确定用什么词来形容这个词最好,所以我将直接进入一个例子 a bunch of lines we don't care about [...] This is a nice line I can look for This is the string I wish to extract a bunch more lines we do't care about [...] This line contains an integer 12345 related to the string abo

我不确定用什么词来形容这个词最好,所以我将直接进入一个例子

a bunch of lines we don't care about [...]
This is a nice line I can look for
This is the string I wish to extract
a bunch more lines we do't care about [...]
This line contains an integer 12345 related to the string above
more garbage [...]
但有时(我无法控制这一点)订单被交换:

a bunch of lines we don't care about [...]
Here is another string I wish to extract
This is a nice line I can look for
a bunch more lines we do't care about [...]
This line contains an integer 67890 related to the string above
more garbage [...]
两行(“漂亮的行”和“我想提取的字符串”)总是相邻的,但顺序是不可预测的。包含行的整数下面的行数不一致。“nice line”出现多次,并且始终相同,并且我(全局)提取的字符串和整数可能相同或不同

最终的想法是填充两个列表,一个包含字符串,另一个包含整数,两者都按找到时的顺序排列,以便以后可以将两者用作键/值对

我不知道怎么做,或者即使可能,也不知道如何编写一个正则表达式,在目标行之前或之后立即查找字符串

顺便说一句,在Python中执行此操作

想法

编辑/添加:因此,我期望上述示例文本的结果如下:

list1["This is the string I wish to extract", "Here is another string I wish to extract"]
list2[12345, 67890]

一个好的策略可能是寻找“漂亮的线条”,然后搜索上面和下面的线条

请参阅以下(未经测试的)python psuedocode:

L1, L2 = [], []
lines = open("file.txt").readlines()
for i, line in enumerate(i, lines):
    if 'nice line' in line:
       before_line = lines[min(i-1, 0)]
       after_line = lines[min(i+1, len(lines) - 1)]
       # You can generalize the above to a few lines above and below

       # Use regex to parse information from `before_line` and `after_line`
       # and add it to the lists: L1, L2

你需要把它推广到两行以上吗?你能给出一些你想看到的具体例子吗?@KarlKnechtel在这个例子中,没有。只有两行。@StefanGruenwald我编辑了我的问题。实际上,我不再需要这方面的帮助,因为在这个特殊的例子中,我注意到我需要搜索的行有一些独特的地方,这使得工作非常容易。尽管如此,困惑依然存在,未来可能会有一个有用的答案。jp24的方法当然是足够的,但如果存在更广义的正则表达式(或其他)方法。。。好吧,我会把它打开一会儿。