简体   繁体   English

python regex用匹配的字符串替换所有出现的事件

[英]python regex replace all occurances with the matched string

I have a document i'm trying to display it in a jinja template. 我有一个文档,试图将其显示在Jinja模板中。 I am trying to replace the string like this negxxx string xxxneg needs to become <span class="SomeCssClass_neg_xxx"> string </span> . 我正在尝试替换此字符串,例如negxxx string xxxneg需要变为<span class="SomeCssClass_neg_xxx"> string </span> the problem is with the matched group numbering \\1 that i am using. 问题是我正在使用匹配的组编号\\1 I know i have multiple matches not only 1. need some help. 我知道我不仅有多次比赛,还需要一些帮助。

import re
StringIn = 'negxxx data1 xxxneg  out of span negxxx data2 xxxneg negzzz data1 zzzneg  out of span negzzz data2 zzzneg'
StringIn = re.sub(r"negxxx(.*)xxxneg", r"<span class='neg_xxx'>\1</span>" , StringIn)
StringIn = re.sub(r"negzzz(.*)zzzneg", r"<span class='neg_zzz'>\1</span>" , StringIn)
print StringIn

I get: 我得到:

<span class='neg_xxx'> data1 xxxneg  out of span negxxx data2 </span> <span class='neg_zzz'> data1 zzzneg  out of span negzzz data2 </span>

which is not correct, what i need is: 这是不正确的,我需要的是:

<span class='neg_xxx'> data1 </span>   out of span <span class='neg_xxx'> data2 </span><span class='neg_zzz'> data1 </span>  out of span <span class='neg_zzz'> data2 </span>

your .* is racing to the end of the string and only backing up to nearest (to end of string) "xxxneg". 您的.*正在加速到字符串末尾,并且仅备份到最近的(到字符串末尾)“ xxxneg”。 Use a lazy quantifier, .*? 使用惰性量词.*? , which will eat one character at a time only and then try to match the rest of the pattern: ,它将一次只吃一个字符,然后尝试匹配其余模式:

import re
StringIn = 'negxxx data1 xxxneg  out of span negxxx data2 xxxneg negzzz data1 zzzneg  out of span negzzz data2 zzzneg'
StringIn = re.sub(r"negxxx(.*?)xxxneg", r"<span class='neg_xxx'>\1</span>" , StringIn)
StringIn = re.sub(r"negzzz(.*?)zzzneg", r"<span class='neg_zzz'>\1</span>" , StringIn)
print StringIn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM