Understanding Python Regular Expression Recursion

Asked 1 years ago, Updated 1 years ago, 64 views

About Python regular expressions.

interabc(inta){
    if(a>0){
        return1;
    } else {
        return 0;
    }
}
intxyz()
    { return aPtr->type;}

I'm trying to replace the text like the one above as follows:

interabc(inta);
intxyz();

So I wrote the following code:

pattern=r'(\)\s*\s*(?:.+(?R))*.*\s*\})'
content=regex.sub(pattern,r');',content)

However, it does not replace as intended, and the results are as follows:

interabc(inta){
    if(a>0);else{
        return 0;
    }
}
intxyz();

I understand that (?:...) does not end when it matches ... but why does it not match {} on the outside?

I've changed a lot of patterns, but I don't understand the basics, so the result doesn't work as intended...

python regular-expression

2022-09-30 19:46

1 Answers

To begin with the conclusion, the questioner has almost reached the point where the answer is correct, and I think you can get the desired result by using the following pattern.

pattern=r'\)*\s*\{\s*(?:.+(?R))*.*\s*\}'

The only change was to match the first \) against "0 or more".

\R refers to the entire pattern, so the non-termination condition for the part (?:...) is a balanced {} string starting with ). The part of else{...} in the input text is to balanced


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.