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
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
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Understanding How to Configure Google API Key
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.