[英]python pattern match want string after the match
I have a string like .for $i = 5 .. 13
in the dict['comment']
. 我在
dict['comment']
有一个类似.for $i = 5 .. 13
的字符串。 i want get the string after the $i =
my output would be like 5 .. 13
我想在
$i =
之后获取字符串,我的输出将是5 .. 13
i have tried with 我尝试过
if re.match('.for',dict['comment']):
p=re.match('.for',dict['comment'])
print '%s' %p.group(0)
this match giving only patterns matched string. 这种匹配只给出模式匹配的字符串。
i am getting output as 我正在作为输出
.for
.for
.for
but i want output after the .for $i = 但是我想在.for $ i =之后输出
suggestions please 建议
You need to capture the text which exists next to = 您需要捕获=旁边的文本
p = re.match(r'.for[^=]*=\s*(.*)',dict['comment'])
if p:
print '%s' %p.group(1)
You could also use the native string.split()
if the entire string is .for $i = 5 .. 13
: 如果整个字符串都是
.for $i = 5 .. 13
也可以使用本机string.split()
:
s = dict['comment']
if '.for' in s:
output = s.split('=')[1]
#output is then: " 5 .. 13"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.