如何在特定单词后捕获字符串中的最新注释?

时间:2014-05-07 18:40:17

标签: python

我正在尝试格式化下面字符串变量中的数据以打印“Patchset lock”之后的最新评论,我在下面给出了预期输出,有人可以建议如何修复它吗?

string = '''Patch Set 1:

This change is being verified in bugsfor the following manifests along with other changes as detailed below:



=====================================================================================

git-android.comp.com/platform/manifest:kk:default.xml

=====================================================================================

https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217513

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  o https://review-android.comp.com/#change,



Please note that verification of all changes in this batch need to be successful before this change can be merged.

PLEASE DO NOT UPLOAD A NEW PATCH SET, OR REMOVE APPROVALS UNTIL THE VERIFICATION IS COMPLETE.

Patch Set 1: Verified

Successful bugsverification. You may find the results of the verification by following the link(s) below:

https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217513

Patch Set 2: Failed

Failed bugsverification. You may find the results of the verification by following the link(s) below:

https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523'''



    '''

预期输出: -

Failed bugsverification. You may find the results of the verification by following the link(s) below:

https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523"

'''

2 个答案:

答案 0 :(得分:1)

不确定你的意思(顺便说一下你的第一个代码有IndentationError),但这可能会起作用:

>>> print(string.rsplit('Patch Set ', 1)[1].split('\n', 1)[1])

Failed bugsverification. You may find the results of the verification by following the link(s) below:

https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523

说明:.rsplit('Patch Set ', 1)[1]Patch Setrsplit)的最后一次出现时分割,得到第二部分([1]),.split('\n', 1)[1]分裂第一次出现\nsplit)并得到第二个标准杆([1]

答案 1 :(得分:0)

它有点棘手。

Basicly:

  • 搜索"Patch Set "
  • 的最后一个字符串位置
  • 创建一个子串,从此位置结束
  • 搜索第二个职位\n;
  • 再次,从这个位置到结束
  • 创建一个子串

不知道Python中的函数,但基本上应该这样做。