如何在python3中的第一个定界符之前,第一个和最后一个定界符之间以及最后一个定界符之后提取字符串?

时间:2020-02-17 04:46:14

标签: python regex python-3.x

我有一个脚本,其返回形式为-

的字符串
   http-STYPE-CONNECT-new/mydir/20200215_14-30-01/http/PAY_STYPE-CONNECT-new/ "Check for atleast one http 200": Expression '($1=="r" && $13==200)' matched 0 time(s), which is below specified min of 1
   http-PARALLEL-new/mydir/20200215_14-30-01/http/PAY_PARALLEL-new/ "Check for atleast one http 200": Expression '($1=="r" && $13==200)' matched 0 time(s), which is below specified min of 1
   http-STYPE-new/mydir/20200215_14-30-01/http/PAY_STYPE-new/ "Check for atleast one http 200": Expression '($1=="r" && $13==200)' matched 0 time(s), which is below specified min of 1
   http-STYPE-CONNECT-0RTT-new/mydir/20200215_14-30-01/http/PAY_STYPE-CONNECT-0RTT-new/ "Check for atleast one http 200": Expression '($1=="r" && $13==200)' matched 0 time(s), which is below specified min of 1

我想以这样一种方式分割(或应用正则表达式)每个字符串,以使第一个'/'之前的所有 存储在变量test_name中,而所有之间的所有< / em>路径的第一个'/'和最后一个'/' 存储在变量path中,并且路径的最后一个'/'之后的所有内容存储在变量failure_reason中。

示例:

test_name = http-STYPE-CONNECT-new

path = /mydir/20200215_14-30-01/http/PAY_STYPE-CONNECT-new/

failure_reason = "Check for atleast one http 200": Expression '($1=="r" && $13==200)' matched 0 time(s), which is below specified min of 1

还有另外两种可能使解析变得复杂-

  1. 字符串之间的任何地方都可能有\n。例如:路径的最后一个'/'之后。
  2. 字符串中可以有一个'/',它可能出现在路径的最后一个'/'之后。

由于我是新手,所以我只是尝试在python3中使用split('/'), join(), strip()函数,但无法真正获得想要实现的目标。

任何帮助将不胜感激。谢谢




0 个答案:

没有答案