簡體   English   中英

python模式匹配想要匹配后的字符串

[英]python pattern match want string after the match

我在dict['comment']有一個類似.for $i = 5 .. 13的字符串。 我想在$i =之后獲取字符串,我的輸出將是5 .. 13

我嘗試過

if re.match('.for',dict['comment']):
    p=re.match('.for',dict['comment'])
    print '%s' %p.group(0)

這種匹配只給出模式匹配的字符串。
我正在作為輸出

.for
.for
.for

但是我想在.for $ i =之后輸出

建議

您需要捕獲=旁邊的文本

p = re.match(r'.for[^=]*=\s*(.*)',dict['comment'])
if p:
    print '%s' %p.group(1)

如果整個字符串都是.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM