How to replace non-character surrounded string in python

I have a variable with a collection of arguments (newArgs), I want to replace any occurrence of a certain string inside this variable. I can get into my if condition using the re.search command:

if ( re.search(item, newArgs) ):

the ‘if’ condition wont do anything if the condition found the exact item inside the newArgs

when it enters the ‘else’ condition, it should trigger the re.sub command:

Not working:

newArgs = re.sub(argVariable+’=r(S*S)?’, item, newArgs)

Note: I can get the ‘argVariable’ which in this sample valued as “-Dweblogic.Stderr”

How can I replace the matching string with the new ‘item’ inside the ‘newArgs’?

Sample string to be replaced:

-Dweblogic.Stderr=/tmp/error.out

I want to search anything beyond the = sign and it will stop until it sees a whitespace (because going beyond the whitespace is for the next argument) or if it is the end of the newArgs variable

I want it to be replaced with something like:

-Dweblogic.Stderr=/apps/some/other/location/error.out


Source: regex

Leave a Reply