I am trying to parse some C# code to Python. There are several regular expression in that code. Everything worked fine so far, but now I’ve got the following problem:
C# Code:
Match m = regEx.Match( Expression );
while( m.Success )
{
Expression = Expression.Replace( m.Value, m.Groups[1].Value + this.Solve( m.Groups[2].Value ) );
}
What can I do to make this code working in python? I’ve already tried something like this:
matchObj = re.search(pattern = p, string = expression, flags = re.IGNORECASE)
while matchObj:
if len(matchObj.group(3)) > 0:
expression = re.sub(pattern = p, repl = matchObj.group(1) + self.solve(matchObj.group(2)), string = matchObj.string, flags = re.IGNORECASE) #Here is the problem...
So actually I am looking for something equivalent of matchObject.Value.
Thank you for your help.
I think this is what you are trying to do;
match.group()(without a parameter) returns the match the whole regex matches:I’m not sure why you had a loop there, so I removed it. Another way, without using
str.replacewould be to manipulate the string based on the positions returned bym.start()andm.end().