简体   繁体   中英

python regex replace all occurances with the matched string

I have a document i'm trying to display it in a jinja template. I am trying to replace the string like this negxxx string xxxneg needs to become <span class="SomeCssClass_neg_xxx"> string </span> . the problem is with the matched group numbering \\1 that i am using. 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". 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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