[英]Extract string using regex in Python
I'm struggling a bit on how to extract (ie assign to variable) a string based on a regex. 我在如何基于正则表达式提取(即分配给变量)字符串方面有些挣扎。 I have the regex worked out -- I tested on regexpal.
我已经解决了正则表达式-我在正则表达式上进行了测试。 But I'm lost on how I actually implement that in Python.
但是我迷失了如何在Python中实际实现它。 My regex string is:
我的正则表达式字符串是:
http://jenkins.mycompany.com/job/[^\s]+
What I want to do is take string and if there's a pattern in there that matches the regex, put that entire "pattern" into a variable. 我想做的是获取字符串,如果其中有一个与正则表达式匹配的模式,则将整个“模式”放入变量中。 So for example, given the following string:
因此,例如,给定以下字符串:
There is a problem with http://jenkins.mycompany.com/job/app/4567. We should fix this.
I want to extract http://jenkins.mycompany.com/job/app/4567
and assign it a variable. 我想提取
http://jenkins.mycompany.com/job/app/4567
并为其分配一个变量。 I know I'm supposed to use re but I'm not sure if I want re.match or re.search and how to get what I want. 我知道我应该使用re,但是我不确定我是否想要re.match或re.search以及如何获得想要的东西。 Any help or pointers would be greatly appreciated.
任何帮助或指针将不胜感激。
import re
p = re.compile('http://jenkins.mycompany.com/job/[^\s]+')
line = 'There is a problem with http://jenkins.mycompany.com/job/app/4567. We should fix this.'
result = p.search(line)
print result.group(0)
Output: 输出:
http://jenkins.mycompany.com/job/app/4567.
使用re.findall
方法选择字符串中的第一个找到的匹配项:
re.findall(pattern_string, input_string)[0] # pick the first match that is found
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.