使用regex在带有整数值的字符串的引号中查找值

2024-05-29 04:10:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串:

Started by upstream project "fcm-dummy-web" build number 99
originally caused by:
 Started by user Kaul, Kuber
[EnvInject] - Loading node environment variables.
Building on master in workspace /var/lib/jenkins/jobs/mischief-managed/workspace
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /usr/bin/git config remote.origin.url
Fetching upstream changes from https://xx/kaulk/mischief-managed.git
 > /usr/bin/git --version # timeout=10
using GIT_SSH to set credentials 

我需要在第一行中找到作业名称,在这里是“fcm dummy web”和内部版本号“99”。现在,对于不同的工作,这些可能会在不同的构建中发生变化,但在所有情况下,第一行将以“startedbyupstreamproject”开头,后面是“buildnumber”值。正则表达式能找到它吗?你知道吗

我在试:火柴=关于芬德尔(r“^Started by upstream project.*$”,text)未成功。你知道吗


Tags: gitprojectwebbybinusrtimeoutworkspace
3条回答
re.findall('^Started by upstream project "(.+)" build number (\d+)')

您可以这样搜索:

import re
text = '''
Started by upstream project "fcm-dummy-web" build number 99
originally caused by:
 Started by user Kaul, Kuber
'''
m = re.search(r'Started by upstream project "([^"]+)" build number (\d+)', text)
print("project = %s, build number %d" % (m.group(1), int(m.group(2))))

每当正则表达式中使用锚点时,使用多行修饰符m。你知道吗

>>> re.findall(r'(?m)^Started by upstream project\s+"([^"]*)"\s+build number\s+(\d+)', s)
[('fcm-dummy-web', '99')]

DEMO

相关问题 更多 >

    热门问题