[Iteration:0][00:00:0.063] [TestR-6ren">
gpt4 book ai didi

python - 如何在python中使用正则表达式提取两个子字符串之间的字符?

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:12 24 4
gpt4 key购买 nike

例如-我的字符串是:

str1="[00:00:0.047] [TestRunner] [Main] [sleep_assoc] > [Iteration:0][00:00:0.063] [TestRunner] [Main] [sleep_assoc] > [!Iteration:0][00:00:0.063]"

我想将 [Iteration:0][!Iteration:0] 之间的所有字符存储到字符串变量中。

最佳答案

您可以使用 .*? 来完成此操作,它匹配 [Iteration:0] 中的所有字符,直到找到 [!Iteration:0]:

In [1]: import re

In [2]: s = '[00:00:0.047] [TestRunner] [Main] [sleep_assoc] > [Iteration:0][00:00:0.063] [TestRunner] [Main] [sleep_as
...: soc] > [!Iteration:0][00:00:0.063]'

In [3]: m = re.search(r'\[Iteration:0](.*?)\[!Iteration:0]', s)

In [4]: res = m.group(1)

In [5]: res
Out[5]: '[00:00:0.063] [TestRunner] [Main] [sleep_assoc] > '

关于python - 如何在python中使用正则表达式提取两个子字符串之间的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42577435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com