Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式查找字符串中带整数值的引号中的值_Python_Regex_Python 2.7_Regex Greedy - Fatal编程技术网

Python 使用正则表达式查找字符串中带整数值的引号中的值

Python 使用正则表达式查找字符串中带整数值的引号中的值,python,regex,python-2.7,regex-greedy,Python,Regex,Python 2.7,Regex Greedy,我有一个字符串: 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

我有一个字符串:

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虚拟web”,构建编号为“99”。现在,对于不同的作业,这些可能会在不同的构建中发生变化,但在所有情况下,第一行都将以“由上游项目启动”,后面是“构建编号”,后面是值。用什么正则表达式才能找到它


我正在尝试:matches=re.findall(r“^由上游项目启动。*$”,text),但没有成功

您可以这样搜索:

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')]

re.findall(“^由上游项目(“[^”]+”)\s+版本号\s+([\d]+)$)启动)
它为两组匹配。第一组为项目名称,第二组为构建编号,以及如何查找作业名称?
fcm虚拟web
没有整数?@AvinashRaj:请澄清一件事,
^
通常会匹配字符串的开头,那么
(?m)的目的是什么
?这是在正则表达式内部添加多行修饰符的另一种方法。
>>> re.findall(r'(?m)^Started by upstream project\s+"([^"]*)"\s+build number\s+(\d+)', s)
[('fcm-dummy-web', '99')]