I have the following (simplified) code - this is the essence of what i would like to do
def replace_todo(m):
m[2] = '*' if value else ' '
return m.group()
I want to edit a specific group in the match object, and then render the whole match out including the edited part.
The replace_todo function gets called on every match by using
myCompiledRegex.sub(replace_todo, text)
The text that is used as input is
[ ] mah lalalalalaa
[*] mah lalalalalaa
expected output
[*] mah lalalalalaa
[*] mah lalalalalaa
The regex looks like this
^(\[( |\*)]) ([a-z][a-z][a-z]) (.*)$
But it seems i'm not allowed to edit the match object
When i try to execute the above code i get the following TypeErro开发者_如何学Gor
_sre.SRE_Match' object does not support item assignment
Thanks in advance :)
EDIT:
Using spicavigo solution, when converting the list to a string (str(groups)) this is how it looks
[u’[ ]’, '*’, u’jsp’, u’do something’]
this is how it should look
[*] jsp do something
I am novice when it comes to regex but couldn't you take m.groups() initially, change whatever you wish to and then return the variable?
Say:
x=list(m.groups()) #NOTE: groups and not group
x[2] = '*' if value else ' '
return x
EDIT
Maybe now I get what you are trying to achieve. As mentioned in the comment below, can you change your regex to
'^(\[)( |\*)(]) ([a-z][a-z][a-z]) (.*)$'
This is basically to help in joining of the list m.groups()
精彩评论