我有以下输入:
str = """
Q: What is a good way of achieving this?
A: I am not sure. Try the following:
1. Take this first step. Execute everything.
2. Then, do the second step
3. And finally, do the last one
Q: What is another way of achieving this?
A: I am not sure. Try the following alternatives:
1. Take this first step from before. Execute everything.
2. Then, don't do the second step
3. Do the last one and then execute the above step
"""
我想捕获输入中的 QA 对,但我无法获得一个好的正则表达式来执行此操作。我管理了以下内容:
(?ms)^[\s#\-\*]*(?:Q)\s*:\s*(\S.*?\?)[\s#\-\*]+(?:A)\s*:\s*(\S.*)$
但是,我能够捕获如下输入:
('Q', 'What is a good way of achieving this?')
('A', "I am not sure. Try the following:\n 1. Take this first step. Execute everything.\n 2. Then, do the second step\n 3. And finally, do the last one\n\n Q: What is another way of achieving this?\n A: I am not sure. Try the following alternatives:\n 1. Take this first step from before. Execute everything.\n 2. Then, don't do the second step\n 3. Do the last one and then execute the above step\n")
注意第二个 QA 对是如何被第一个捕获的。如果我在答案正则表达式的末尾使用贪婪?
,它不会捕获枚举。关于如何解决这个问题的任何建议?